【发布时间】:2019-05-13 23:49:06
【问题描述】:
我正在尝试将每个子数组中的最大数返回到一个新数组。我觉得我真的很亲近,我觉得if(temp[j] < x) {continue;} 格格不入。我究竟做错了什么?附:我知道我可能只使用 math.max() 并节省大量代码,但我正在努力适应 for 循环和数组。
function largestOfFour(arr) {
let newArr = [];
for(let i = 0; i < arr.length; i++) {
let temp = arr[i];
let counter = 0;
for(let j = 0; j < temp.length; j++) {
let x = 0;
if(temp[j] > counter) {
counter = temp[j];
if(counter > x) {
x = counter;
if(temp[j] < x) {
continue;
}
}
newArr.push(temp[j]);
}
}
console.log(arr[i])
}
console.log(newArr);
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
【问题讨论】:
-
请同时添加想要的结果(哪个方向?)。
标签: javascript arrays for-loop if-statement