简体   繁体   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);

The output should be: 17 -6 but instead, I am getting 17 5. what's wrong with my code? output 应该是: 17 -6 但相反,我得到 17 5. 我的代码有什么问题?

The second if statement is wrong it should compare to min第二个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.

相关问题 数组中有值,但无法访问它们,这可能是什么问题? - Array has values in it, but not able to access them, what might be the problem? 无法从Openweathermap API获取温度 - Can't get temperature from Openweathermap API 无法对数组中的数据进行排序。 排序不是 function - Can't Sort data from an array. sort is not a function 我有两个数组。 我想以一种可以从另一个数组中获取最接近的值的方式比较它们 - I have two array. I want to compare both of them in a way that I can get the nearest value from the other array 无法从数组(Javascript)中获取最小值? - Cannot get minimum value from Array (Javascript)? 如何以最小的复杂性从JavaScript中的数组获取不匹配的数组值? - How to get the unmatched array value from array in javascript with minimum complexity? 函数仅保存并打印数组中的一个值。 怎么修好? - Function saves and prints only one value from array. how it can be fixed? 无法在 JSPDF 中获取 select 值 - can't able to get the select value in JSPDF 从 JSON 文件中的对象数组中获取最小值 - Get Minimum Value From Array of Objects from JSON file 如何在不调用键的情况下从对象数组中获取值(我通过互联网搜索找不到与此完全相同/类似的问题) - How to get a value from an Array of object without calling the key (i've searched through the internet can't found the exact/similar problem to this)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM