У меня возникла проблема с назначением значений массиву, и когда я позже получаю доступ к этому слоту массива, он возвращает NaN.Возврат массива NaN
Во-первых, я объявляю массивы, как:
var oldTherms = [];
var newTherms = [];
var oldInputTherms;
var newInputTherms;
Я использую набор данных переменных для вычисления значения, назначаемую к oldTherms и newTherms массивов. Я проверил, что часть расчета работает и поставляет соответствующее значение в oldInputTherms и newInputTherms. Я также подтвердил, что dataSet.month [i] возвращает правильные значения. Однако приведенная ниже инструкция переключения не добавляет кумулятивного итога к oldTherms [] или newTherms []. Когда я пытаюсь получить доступ к newTherms [] или oldTherms [], то результат будет 'NaN'
switch (dataSet.month[i]){
//subtract 1 in array slot bc months number 1-12 and array slots number 0-11
case 1: //january
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 2: //february
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 3: //march
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 4: //april
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 5: //may
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 6: //june
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 7: //july
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 8: //august
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 9: //sept
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 10: //oct
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 11: //nov
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
case 12: //dec
oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
newTherms[dataSet.month[i]-1] += newInputTherms;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
break;
default:
oldTherms[dataSet.month[i]-1] += 0; //add therms to the total used
newTherms[dataSet.month[i]-1] += 0;
oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
newKwh[dataSet.month[i]-1] += 0;
}
Зачем вам нужны все те случаи, которые делают то же самое? – Barmar
'oldInputTherms',' newInputTherms' объявлены, но не определены – Drakes
В чем смысл добавления '0' ко всему в случае' default: '? – Barmar