簡體   English   中英

無法從溫度數組中獲取最小值。 可能是什么問題?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM