Javascript 在数组javascript中找到最大的整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13794225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
finding largest integer in array javascript
提问by George Agusta
Possible Duplicate:
How might I find the largest number contained in a JavaScript array?
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 can someone help me figure out what I did wrong. Here is my code:
我在让这段代码工作时遇到了麻烦我已经研究了一段时间试图弄清楚它。当我查看控制台时,它只显示 0 谁能帮我弄清楚我做错了什么。这是我的代码:
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);
采纳答案by Yamaha32088
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 arrayin the if statement.
下面的代码是固定的,应该可以工作。问题是在这一行中if (array>largest) {您没有提供数组的索引。通过将代码更改为此,if (array[i]>largest) {它可以工作。请注意,我在 if 语句[i]的末尾添加了array。
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);
回答by Larry Battle
var arr = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = arr[0];
for (var i = 0; i < arr.length; i++) {
if (largest < arr[i] ) {
largest = arr[i];
}
}
console.log(largest);
- You need to define
ior else it become a global variable. - Don't redefine largest in the loop.
- Since you're looping through the array, use
i < array.lengthinstead ofi <= largest. - Since you're comparing each of the items in the array to
largest, useif(largest < array[i])instead ofif(array > largest) - You should set largest equal to the first element in the array because what if all the numbers are negative?
arrayis a bad variable name because it's too similar toArray(the array constructor). Tryarrinstead.
- 您需要定义
i,否则它会成为一个全局变量。 - 不要在循环中重新定义最大。
- 由于您正在遍历数组,因此请使用
i < array.length代替i <= largest. - 由于您将数组中的每个项目与 进行比较
largest,请使用if(largest < array[i])而不是if(array > largest) - 您应该设置最大等于数组中的第一个元素,因为如果所有数字都是负数呢?
array是一个糟糕的变量名,因为它与Array(数组构造函数)太相似了。试试吧arr。
One liner:
一个班轮:
var largest = Math.max.apply(0, array);
var largest = Math.max.apply(0, array);
More info here: Javascript max() function for 3 numbers
更多信息在这里:Javascript max() 函数用于 3 个数字
回答by dmi3y
Just one line :)
只有一行 :)
var array = [3 , 6, 2, 56, 32, 5, 89, 32],
largest = array.sort((a,b)=>a-b).reverse()[0];
or even better
甚至更好
...
largest = array.sort((a,b)=>a-b)[array.length - 1];
UPD, all code above is sucks when you add for example 9 in array my guess because by default numbers treated as strings in sort, there is better version
UPD,当您在数组中添加例如 9 时,上面的所有代码都很糟糕,我的猜测是因为默认情况下,数字被视为排序中的字符串,有更好的版本
var array = [3 , 6, 2, 56, 32, 5, 89, 32, 9], largest;
array.sort(function(a, b) {
largest = a > b ? a: b;
});
although in performance wise forEachloop suggested in comments are better
http://jsperf.com/array-sorting-javascript-stack
虽然在forEach评论中建议的性能明智的循环更好
http://jsperf.com/array-sorting-javascript-stack
UPD2, okay, code above has some bad parts in it, so will not work as expected. Another try:
UPD2,好吧,上面的代码有一些不好的部分,所以不会按预期工作。另一个尝试:
array.sort(function(a, b) {
return a - b;
});
largest = array[array.length - 1];
回答by zongweil
You have a few small mistakes. First:
你有几个小错误。第一的:
if (array>largest) {
It should instead be:
它应该是:
if ( array[i]>largest) {
Second:
第二:
for ( i = 0; i <= largest; i++) {
should be
应该
for (i = 0; i <= array.length; i++) {
回答by jermel
var array = [3 , 6, 2, 56, 32, 5, 89, 32];
var largest= array[0];
for (i=0; i<=largest;i++){
if (array[i]>largest) {
var largest=array[i];
}
}
回答by tvanfosson
You have two issues in your code. First, array>largestshould be array[i]>largest. Second, you are declaring a new largest variable inside the ifwhich isn't the same as the one outside. Remove varfrom the assignment of the new largest value.
您的代码中有两个问题。首先,array>largest应该是array[i]>largest。其次,您在内部声明了一个新的最大变量,if该变量与外部的不同。var从新的最大值的分配中删除。

