繁体   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