简体   繁体   中英

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);

The output should be: 17 -6 but instead, I am getting 17 5. what's wrong with my code?

The second if statement is wrong it should compare to 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM