查找数组中的最大值 (array.find)
Finding max value in array (array.find)
我正在学习 Javascipt,实际上我正在学习数组方法。
我的想象练习依赖于通过 array.find 方法在数组中找到 Max/Min 值。
实际上我是那样做的,但是脚本返回了我 "Undefined"。
请帮忙。 :)
const scores = [10, 20, 30, 22, 25, 109, 90];
const maxScore = scores.find(score => {
let max = 0;
for (let i=1; i < scores.length; i++){
if(score[i] > max){
max = score[i];
};
};
return max;
});
console.log(maxScore);
P.S。我知道 "Math.max.apply",但我必须通过 array.find 和简单的循环来完成。
find
适用于每个数组元素。因此,将 max
放在 find
方法之外 & log
max
。另外还有两个错别字
const scores = [10, 20, 30, 22, 25, 109, 90];
let max = 0;
const maxScore = scores.find((score) => {
for (let i = 1; i < scores.length; i++) {
if (scores[i] > max) {
max = scores[i];
};
};
return max;
});
console.log(max)
试试这个:
const scores = [10, 20, 30, 22, 25, 109, 90];
let max = 0;
scores.find(score => { if(score > max) max = score });
console.log(max);
您当前的代码正在循环 scores 数组,而它已经在循环它,JavaScripts .find,本质上是在循环数组。
最简单的方法,不使用任何数组方法,可以写成:
const maxScore = (scores) => {
let score = 0;
for ( let i = 0; i < scores.length; i++ ) {
if(scores[i] > score) {
score = scores[i]
}
}
return score;
}
来自MDN:
The find() method returns the value of the first element
in the provided array that satisfies the provided testing function.
让我们再次重新定义我们的简单函数,
const maxScore = scores => {
let score = Number.NEGATIVE_INFINITY;
scores.forEach(element => {
let acc = scores.find(number => number > score);
if(!isNaN(acc)) {
score = acc;
}
})
return score;
}
您可以对索引进行闭包以从末尾开始循环,并在开始时未定义并从第一个元素获取第一个值的临时最大值。
然后在 temp index 的值小于 score
时循环,将此值存储在 max
中,重复。
最后 return 索引加一等于临时索引的结果。
这种方法需要一个循环。 find
从数组的开头开始迭代,如果两个索引交叉,则从数组的末尾开始循环,找到结果。
const
scores = [100, 20, 30, 22, 25, 109, 90],
maxScore = scores.find(
((j, max) => (score, i, array) => {
if (max === undefined) {
max = score;
j = array.length;
}
if (score < max) return;
while (array[j - 1] < score) max = array[--j];
return i + 1 === j;
})
()
);
console.log(maxScore);
const scores = [10, 20, 30, 22, 25, 109, 90];
scores.reduce(function(a,b) { return a > b ? a : b });
// 109
我正在学习 Javascipt,实际上我正在学习数组方法。 我的想象练习依赖于通过 array.find 方法在数组中找到 Max/Min 值。
实际上我是那样做的,但是脚本返回了我 "Undefined"。 请帮忙。 :)
const scores = [10, 20, 30, 22, 25, 109, 90];
const maxScore = scores.find(score => {
let max = 0;
for (let i=1; i < scores.length; i++){
if(score[i] > max){
max = score[i];
};
};
return max;
});
console.log(maxScore);
P.S。我知道 "Math.max.apply",但我必须通过 array.find 和简单的循环来完成。
find
适用于每个数组元素。因此,将 max
放在 find
方法之外 & log
max
。另外还有两个错别字
const scores = [10, 20, 30, 22, 25, 109, 90];
let max = 0;
const maxScore = scores.find((score) => {
for (let i = 1; i < scores.length; i++) {
if (scores[i] > max) {
max = scores[i];
};
};
return max;
});
console.log(max)
试试这个:
const scores = [10, 20, 30, 22, 25, 109, 90];
let max = 0;
scores.find(score => { if(score > max) max = score });
console.log(max);
您当前的代码正在循环 scores 数组,而它已经在循环它,JavaScripts .find,本质上是在循环数组。
最简单的方法,不使用任何数组方法,可以写成:
const maxScore = (scores) => {
let score = 0;
for ( let i = 0; i < scores.length; i++ ) {
if(scores[i] > score) {
score = scores[i]
}
}
return score;
}
来自MDN:
The find() method returns the value of the first element
in the provided array that satisfies the provided testing function.
让我们再次重新定义我们的简单函数,
const maxScore = scores => {
let score = Number.NEGATIVE_INFINITY;
scores.forEach(element => {
let acc = scores.find(number => number > score);
if(!isNaN(acc)) {
score = acc;
}
})
return score;
}
您可以对索引进行闭包以从末尾开始循环,并在开始时未定义并从第一个元素获取第一个值的临时最大值。
然后在 temp index 的值小于 score
时循环,将此值存储在 max
中,重复。
最后 return 索引加一等于临时索引的结果。
这种方法需要一个循环。 find
从数组的开头开始迭代,如果两个索引交叉,则从数组的末尾开始循环,找到结果。
const
scores = [100, 20, 30, 22, 25, 109, 90],
maxScore = scores.find(
((j, max) => (score, i, array) => {
if (max === undefined) {
max = score;
j = array.length;
}
if (score < max) return;
while (array[j - 1] < score) max = array[--j];
return i + 1 === j;
})
()
);
console.log(maxScore);
const scores = [10, 20, 30, 22, 25, 109, 90];
scores.reduce(function(a,b) { return a > b ? a : b });
// 109