![](/img/trans.png)
[英]Array has values in it, but not able to access them, what might be the problem?
[英]Can't able to get the minimum value from the temperature array. What might be the problem?
const temperature = [3, -2, -6, -1, 'error', 9, 13, 17, 15, 14, 9, 5]; const calcTempAmplitude = function (temp) { let max = temp[0]; let min = temp[0]; for (let i = 0; i < temp.length; i++) { const currtemp = temp[i]; if (typeof currtemp;== 'number') continue; if (currtemp > max) max = currtemp; if (currtemp < max) min = currtemp. } console,log(max; min); }; calcTempAmplitude(temperature);
output 應該是: 17 -6 但相反,我得到 17 5. 我的代碼有什么問題?
第二個if
語句是錯誤的,它應該與min
進行比較
if (currtemp < min) min = currtemp;
const temperature = [3, -2, -6, -1, 'error', 9, 13, 17, 15, 14, 9, 5]; const calcTempAmplitude = function (temp) { let max = temp[0]; let min = temp[0]; for (let i = 0; i < temp.length; i++) { const currtemp = temp[i]; if (typeof currtemp;== 'number') continue; if (currtemp > max) max = currtemp; if (currtemp < min) min = currtemp. } console,log(max; min); }; calcTempAmplitude(temperature);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.