JavaScript:for 循环内的 if 语句(从数组中查找最大数)
JavaScript: if statement inside for loop (finding largest number from array)
我无法理解这段代码如何从数字数组中找到最大的数字。这是我目前的理解:
1) 声明变量i,并赋值0。
2) for 循环:x 从 0 开始,但会在 x < integers.length 内递增(这会是 5?)所以我们会讨论 x = 0 --> 4
3) if 语句是我感到困惑的地方。 "integers"是参数,但是我们传给它的参数是一个数组。所以在我的脑海里,我看到 [5, 10, 21, 6, 100][x] > i。因此,如果它索引 1 --> 4,它们仍将大于 0。
let i = 0;
function findLargest (integers) {
for (let x=0; x<integers.length; x++) {
if (integers[x] > i) {
i = integers[x];
}
}
console.log(i);
}
findLargest([5, 10, 21, 6, 100]);
//Output: 100
什么不合理?这称为数组索引(通过指定索引从数组中获取值)。语法是 array[index]
所以,如果你有一个数组,比如说
var arr = [10, 6, 15]
您可以通过
访问它的第二个元素
arr[1] // 6
数组不需要存储在变量中。数组是 values(此外,它们是对象)。所以 [10, 6, 15][1]
与上面的示例具有完全相同的效果。
在您的例子中,integers[x]
将是位置 x 处的元素。使用 for 0 to length - 1 循环是一种遍历数组的方法。该函数遍历数组中的每个元素并验证该元素是否大于找到的最后一个元素。这样,它找到最大的
注意:你的算法不完整,因为对于所有元素都小于0的数组,它会失败。在这种情况下,搜索不应该从一个开始任意值(在您的情况下为 0),但来自数组的现有元素(通常是第一个)。此外,多次调用函数失败,因为变量 x
是 global 而不是函数的局部变量。我建议删除代码的第一行并在函数开头添加新行:
var i = integers[0]
Update(OP 编辑问题后)
是的,它们都将大于 0,但是您不是每次都搜索大于 0。行
i = integers[x]
将i的值改为最开始发现大于0的元素。所以下一个 serch 将比那个元素更大。看起来像这样。让我们以这个数组为例:[5, 7, 2, 12, 0]
5 > 0 (TRUE) => i = 5
7 > 5 (TRUE) => i = 7
2 > 7 (FALSE)
12 > 7 (TRUE) => i = 12
0 > 12 (FALSE)
您可以获取带索引的数组并转到每个值并检查并查看哪个值用于比较以及哪个值发生变化。
在循环中,您可以添加 console.log
和索引,要比较的两个值,比较结果和 i 的新值(如果更改),如
console.log(x, array[x], i, array[x] > i, array[x] > i && array[x])
[5, 10, 21, 6, 100][x] > i
with values
[5, 10, 21, 6, 100][0] > 0 5 > 0 -> i = 5
[5, 10, 21, 6, 100][1] > 10 10 > 5 -> i = 10
[5, 10, 21, 6, 100][2] > 21 21 > 10 -> i = 21
[5, 10, 21, 6, 100][3] > 6 6 > 21
[5, 10, 21, 6, 100][4] > 100 100 > 21 -> i = 100
function findLargest(integers) {
var i = integers[0], // take the first element of the array
x;
for (x = 1; x < integers.length; x++) { // iterate from the second element
console.log(x, integers[x], i, integers[x] > i, integers[x] > i && integers[x]);
if (integers[x] > i) {
i = integers[x];
}
}
return i;
}
console.log(findLargest([5, 10, 21, 6, 100]));
您也可以使用 reduce 而不是循环。
function findLargest (integers) {
if(integers.length===0){
return;//return undefined for empty array since there is no lowest
}
return integers.reduce(
function(highest,current){
return (current>highest)
? current
: highest
}
);;
};
findLargest([5, 10, 21, 6, 100]);
对于许多可以用 reduce 解决的过程,你也可以使用 recursion:
function findLargest (integers) {
var recur = function(highest,numbers){
if(numbers.length===0){//processed all numbers, return highest
return highest;
}
if(numbers[0]>highest){
highest=numbers[0];
}
//call itself again with highest and numbers without the first element
return recur(highest,numbers.slice(1));
};
//return recursive function recur
return recur(integers[0],integers.slice(1));
};
findLargest([5, 10, 21, 6, 100]);
一种不是很有效但代码量最少的方法(不建议对使用大型数组或多次调用较小数组的代码执行此操作)。
这使用 mutator method called sort.
function findLargest (integers) {
//map integers so you have a copy of integers
// this because .sort is a mutator methods and will
// change integers if you don't copy it first
return integers.map(x=>x).sort((a,b)=>a-b).slice(-1)[0];
};
findLargest([5, 10, 21, 6, 100]);
我无法理解这段代码如何从数字数组中找到最大的数字。这是我目前的理解:
1) 声明变量i,并赋值0。
2) for 循环:x 从 0 开始,但会在 x < integers.length 内递增(这会是 5?)所以我们会讨论 x = 0 --> 4
3) if 语句是我感到困惑的地方。 "integers"是参数,但是我们传给它的参数是一个数组。所以在我的脑海里,我看到 [5, 10, 21, 6, 100][x] > i。因此,如果它索引 1 --> 4,它们仍将大于 0。
let i = 0;
function findLargest (integers) {
for (let x=0; x<integers.length; x++) {
if (integers[x] > i) {
i = integers[x];
}
}
console.log(i);
}
findLargest([5, 10, 21, 6, 100]);
//Output: 100
什么不合理?这称为数组索引(通过指定索引从数组中获取值)。语法是 array[index]
所以,如果你有一个数组,比如说
var arr = [10, 6, 15]
您可以通过
访问它的第二个元素arr[1] // 6
数组不需要存储在变量中。数组是 values(此外,它们是对象)。所以 [10, 6, 15][1]
与上面的示例具有完全相同的效果。
在您的例子中,integers[x]
将是位置 x 处的元素。使用 for 0 to length - 1 循环是一种遍历数组的方法。该函数遍历数组中的每个元素并验证该元素是否大于找到的最后一个元素。这样,它找到最大的
注意:你的算法不完整,因为对于所有元素都小于0的数组,它会失败。在这种情况下,搜索不应该从一个开始任意值(在您的情况下为 0),但来自数组的现有元素(通常是第一个)。此外,多次调用函数失败,因为变量 x
是 global 而不是函数的局部变量。我建议删除代码的第一行并在函数开头添加新行:
var i = integers[0]
Update(OP 编辑问题后) 是的,它们都将大于 0,但是您不是每次都搜索大于 0。行
i = integers[x]
将i的值改为最开始发现大于0的元素。所以下一个 serch 将比那个元素更大。看起来像这样。让我们以这个数组为例:[5, 7, 2, 12, 0]
5 > 0 (TRUE) => i = 5
7 > 5 (TRUE) => i = 7
2 > 7 (FALSE)
12 > 7 (TRUE) => i = 12
0 > 12 (FALSE)
您可以获取带索引的数组并转到每个值并检查并查看哪个值用于比较以及哪个值发生变化。
在循环中,您可以添加 console.log
和索引,要比较的两个值,比较结果和 i 的新值(如果更改),如
console.log(x, array[x], i, array[x] > i, array[x] > i && array[x])
[5, 10, 21, 6, 100][x] > i
with values
[5, 10, 21, 6, 100][0] > 0 5 > 0 -> i = 5 [5, 10, 21, 6, 100][1] > 10 10 > 5 -> i = 10 [5, 10, 21, 6, 100][2] > 21 21 > 10 -> i = 21 [5, 10, 21, 6, 100][3] > 6 6 > 21 [5, 10, 21, 6, 100][4] > 100 100 > 21 -> i = 100
function findLargest(integers) {
var i = integers[0], // take the first element of the array
x;
for (x = 1; x < integers.length; x++) { // iterate from the second element
console.log(x, integers[x], i, integers[x] > i, integers[x] > i && integers[x]);
if (integers[x] > i) {
i = integers[x];
}
}
return i;
}
console.log(findLargest([5, 10, 21, 6, 100]));
您也可以使用 reduce 而不是循环。
function findLargest (integers) {
if(integers.length===0){
return;//return undefined for empty array since there is no lowest
}
return integers.reduce(
function(highest,current){
return (current>highest)
? current
: highest
}
);;
};
findLargest([5, 10, 21, 6, 100]);
对于许多可以用 reduce 解决的过程,你也可以使用 recursion:
function findLargest (integers) {
var recur = function(highest,numbers){
if(numbers.length===0){//processed all numbers, return highest
return highest;
}
if(numbers[0]>highest){
highest=numbers[0];
}
//call itself again with highest and numbers without the first element
return recur(highest,numbers.slice(1));
};
//return recursive function recur
return recur(integers[0],integers.slice(1));
};
findLargest([5, 10, 21, 6, 100]);
一种不是很有效但代码量最少的方法(不建议对使用大型数组或多次调用较小数组的代码执行此操作)。
这使用 mutator method called sort.
function findLargest (integers) {
//map integers so you have a copy of integers
// this because .sort is a mutator methods and will
// change integers if you don't copy it first
return integers.map(x=>x).sort((a,b)=>a-b).slice(-1)[0];
};
findLargest([5, 10, 21, 6, 100]);