So, I have JS code that is meant to acquire data from server about temperature from 2 sensors. The data is contained in a text file where each line carries date and 2 values each for one thermometer. I have managed to parse this data into arrays of dates and values separately. I can find minimum and maximum value of each array, but when I try to get min and max values from both temperature arrays, I get false data. The temperature values are stored in data.temperature
, which is an array containing 2 values, each for on of the thermometers (data
is an array of objects containing a property temperature
, which as an array of two values). I used a debugger and at multiple points the code threw false when comparing two values which was clearly wrong (19>6=false).
Here is 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];
}
}
}
Object extremes
contains indexes of those extremes in data
array. minTempAbsolute
and maxTempAbsolute
contains an array of two indexes - one for data
and one for temperature
.
At one point it was comparing data[i].temperature[j] > data[extremes.maxTempAbsolute[0]].temperature[extremes.maxTempAbsolute[1]]
where i
was 1540 and j
was 0 and maxTempAbsolute[0]
was 460 and maxTempAbsolute[1]
was 1. Therefore:
data[1540].temperature[0] > data[460].temperature[1]
19 > 6
false
When I took these values and logged them in console, it showed me values 19 for the first one and 6 for the second one, but still it would not compare them correctly (as shown above).
My guess is it somehow compares indexes as well because:
data[1540].temperature[1] > data[460].temperature[1]
6.5 > 6
true
When the index changed it was comparing just fine.
Please help me. I have tried everything I know and am lost for ideas. Any help will be greatly appreciated. If there are any questions to my problem I will try to answer them promptly.
Since you read the temperatures from file, you should use Number
constructor because you're comparing strings
.
Number(data[1540].temperature[0]) > Number(data[460].temperature[1])