数组返回undefined =>价值不会被推动

时间:2015-10-30 18:50:17

标签: javascript

function calculate(n1, n2, o) {
var num1 = (n1).toString(); 
var num2 = (n2).toString(); 
getNumber(num1);
getNumber(num2);
var valueArray = [x];
var number1;
var number2;
var counter2;
var newArray = [];
function getNumber(number){
    var addition = 0;
    var counter = num1.length - 1;
    for(var i = 0; i < num1.length; i++){
     if(number[i] != "0"){
        addition += Math.pow(2, counter);
     }
    counter --;
    }
    newArray.push(addition);
}

}
calculate(1101, 1101);

我正在尝试将new的值添加到newArray并收到错误,说&#34;无法读取未定义的属性推送。&#34;有什么想法吗?

2 个答案:

答案 0 :(得分:3)

在初始化阵列之前,您正在调用getNumber(num1);。将行var newArray = [];移到调用getNumber

之上

答案 1 :(得分:0)

基本上&#39; newArray&#39;未初始化。 如果你想保留&#39; newArray&#39;在执行函数之间:

list.remove()