0
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);

I am trying to push the value of addition to newArray and am recieving an error saying "cannot read property push of undefined." Any thoughts?

Blondie
  • 833
  • 5
  • 6
  • Don't forget to accept the answer that helped you the most. While i'm here, you left an answer over here that was correct, i'd suggest undeleting it. :) http://stackoverflow.com/a/33445671/400654 – Kevin B Oct 30 '15 at 22:30

2 Answers2

3

You are calling getNumber(num1); before you initialise the array. Move the line var newArray = []; above your calls to getNumber

bhspencer
  • 13,086
  • 5
  • 35
  • 44
0

Basically 'newArray' is not initialized. If you want to preserve 'newArray' between executions of the function:

if(!newArray)
    newArray = new Array();
newArray.push(addition);