【发布时间】:2015-08-03 21:50:28
【问题描述】:
我在为数组分配值时遇到问题,当我稍后访问该数组槽时,它返回 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;
}
【问题讨论】:
-
为什么你需要所有做同样事情的案例?
-
oldInputTherms,newInputTherms已声明,但未定义 -
在
default:案例中的所有内容中添加0有什么意义? -
将
console.log(oldInputTherms)放在switch语句之前。
标签: javascript arrays nan