在JavaScript中查找数组中的最大整数 [英] Finding largest integer in an array in JavaScript
本文介绍了在JavaScript中查找数组中的最大整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法使此代码正常工作.我已经尝试了一段时间了.当我查看控制台时,它仅显示0.我做错了什么?
I am having trouble getting this code to work. I have been at it for a while trying to figure it out. When I look at the console it just displays 0. What did I do wrong?
这是我的代码:
var array = [3 , 6, 2, 56, 32, 5, 89, 32];
var largest= 0;
for (i=0; i<=largest;i++){
if (array>largest) {
var largest=array[i];
}
}
console.log(largest);
推荐答案
下面的代码是固定的,应该可以运行.问题是在这行 if(array> largest){
中,您没有提供数组的索引.通过将代码更改为此 if(array [i]> largest){
.请注意,我在if语句的 array
末尾添加了 [i]
.
The code below is fixed and should work. The problem was that in this line if (array>largest) {
You were not providing the index of the array. By changing the code to this if (array[i]>largest) {
it works. Notice that I added the [i]
to the end of array
in the if statement.
var array = [3 , 6, 2, 56, 32, 5, 89, 32];
var largest= 0;
for (i=0; i<=largest;i++){
if (array[i]>largest) {
var largest=array[i];
}
}
console.log(largest);
这篇关于在JavaScript中查找数组中的最大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文