返回NaN的数组

时间:2015-05-22 01:44:38

标签: javascript arrays nan

我在向数组分配值时遇到问题,当我稍后访问该数组插槽时,它返回NaN。

首先,我声明数组如:

    var oldTherms = []; 
    var newTherms = [];
    var oldInputTherms;
    var newInputTherms;

我正在使用变量数据集来计算要分配给oldTherms和newTherms数组的值。我已经验证了计算部分的工作原理并为oldInputTherms和newInputTherms提供了适当的值。我还验证了dataSet.month [i]返回正确的值。但是,下面的switch语句似乎没有向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;
}

2 个答案:

答案 0 :(得分:3)

  

当我尝试访问newTherms []或oldTherms []时,结果是' NaN'

oldInputThermsnewInputTherms已声明,但未定义,因此这些行

oldTherms[dataSet.month[i]-1] += oldInputTherms;
newTherms[dataSet.month[i]-1] += newInputTherms; 

会导致你的NaN。

但是,你说

  

我已经验证了计算部分的工作原理并为oldInputTherms和newInputTherms提供了适当的值

如果是这样,则可能oldThermsnewTherms未正确初始化。如果是这种情况,请参阅上面的Barmar答案。

答案 1 :(得分:2)

您需要初始化数组以包​​含0:

var oldTherms = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var newTherms = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 

否则,您要将newInputThermsoldInputTherms添加到undefined,其结果为NaN