Currently diving into the realm of Javascript, I have encountered an issue where my calculated data is returning as NaN.
The desired output should be
ar = [37, 36.63, 35.68, 38.81, 37.67, 37.64, 37.64, 39.74, 40.67, 40.61];
ma = [0.00, 0.63, 3.32, 0.81, 0.33, 0.36, 2.36, 1.26, 0.33, 1.61];
I suspect there might be a flaw in my code causing this unexpected outcome. Can you help me identify it?
var temperature = [37, 36, 39, 38, 38, 38, 40, 41, 41, 39];
var ar = []
var ma = []
var temp = 0
// Calculating AR
for (var i = 1; i < temperature.length; i++) {
ar[0] = temperature[0]
temp = 0.99 * temperature[i - 1] + 0.06 * ma[i - 1]
ar.push(temp)
}
// Calculating MA
for (var j = 0; j < temperature.length; j++) {
temperature[j] = Math.abs(temperature[j] - ar[j]);
ma.push(temperature[j])
}
console.log(ar);
// Output: 37, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN
console.log(ma);
// Output: 0, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN