我有点困惑,为什么下面的函数的return语句只显示我的初始化值。
输入是整数数组,我期望一个[x,y]
值数组,但它一直都在返回初始值[0,0]。
任何想法都请我在这里想念...
const scores = [10, 5, 20, 20]
function breakingRecords(scores) {
let minAndMaxQuantity = [0, 0];
let minAndMax = [0, 0]
scores.map((element, i) => {
if (i == 0) {
// first one is max and 2nd min
minAndMax[0] = element
minAndMax[1] = element
} else {
switch (element) {
case minAndMax[0] < element:
minAndMax[0] = element
minAndMaxQuantity[0] = minAndMaxQuantity[0] + 1;
break;
case minAndMax[0] > element:
minAndMax[1] = element
minAndMaxQuantity[1] = minAndMaxQuantity[1] + 1;
break;
case minAndMax[0] == element || minAndMax[1] == element:
break;
}
}
})
return minAndMaxQuantity;
// returning [0,0] every time
}
console.log(breakingRecords(scores))