Recently, I've been working on a JavaScript code that is designed to retrieve data from a server regarding the temperature readings from 2 sensors. The data is stored in a text file where each line includes a date along with 2 values corresponding to each thermometer. After successfully parsing this data into separate arrays for dates and values, I encountered an issue when trying to identify the minimum and maximum values from both temperature arrays simultaneously. The temperature readings are saved in data.temperature
, which is an array comprising of 2 values, one for each thermometer (data
is an array of objects with a property temperature
holding an array of two values). While using a debugger, there were instances where the code incorrectly evaluated comparisons between two values leading to inaccurate results (e.g., 19 > 6 = false).
Below is a snippet of the code:
extremes.minTempAbsolute = [0, 0];
for(var i = 1; i < data.length; i++){
for(var j = 0; j < data[i].temperature.length; j++){
if(data[i].temperature[j] < data[extremes.minTempAbsolute[0]].temperature[extremes.minTempAbsolute[1]]){
extremes.minTempAbsolute = [i, j];
}
}
}
extremes.maxTempAbsolute = [0, 0];
for(var i = 1; i < data.length; i++){
for(var j = 0; j < data[i].temperature.length; j++){
if(data[i].temperature[j] > data[extremes.maxTempAbsolute[0]].temperature[extremes.maxTempAbsolute[1]]){
extremes.maxTempAbsolute = [i, j];
}
}
}
The object extremes
stores the indexes of these extremes within the data
array. minTempAbsolute
and maxTempAbsolute
consist of an array with two indexes - one referencing data
and the other temperature
.
Upon examination, it was discovered that during a specific comparison,
data[i].temperature[j] > data[extremes.maxTempAbsolute[0]].temperature[extremes.maxTempAbsolute[1]]
, with i
being 1540, j
as 0, maxTempAbsolute[0]
equaling 460, and maxTempAbsolute[1]
set to 1. Consequently:
data[1540].temperature[0] > data[460].temperature[1]
19 > 6
false
Despite verifying these values through console logging, where they displayed as 19 and 6 respectively, the comparison still led to an incorrect outcome.
This discrepancy may be attributed to possibly comparing indexes as well, evident in the following scenario:
data[1540].temperature[1] > data[460].temperature[1]
6.5 > 6
true
When the index was altered, accurate comparisons were achieved seamlessly.
If you have any insights or suggestions to resolve this issue, your assistance would be greatly valued. Feel free to inquire about any additional details related to my dilemma, and I will respond promptly.