Javascript calculator challenge help

Javascript calculator challenge help
0

#1

Hi folks… I’m sure this is really easily solved but looking for help. I’m bumbling my way through this project and trying to push entered digits into an array. It works but replaces rather than pushes the numbers entered into my array… i.e if I click 1,2,3… the console logs the array as [1], then [2], then [3]… whereas I want it to update and show [1,2,3]… Can anyone help with what I am doing wrong?

$(":button").click(function() {

//setup the global variables
var operator = “”;
var firstArr = [];
var result = 0;

//need to push digits entered to an array until we detect an = button

if (($(this).val()) != “=”) {
firstArr.push($(this).val());
}
console.log(firstArr);
$("#screen").html(result);
});


#2

Without seeing the rest of your code, it is difficult to say. Provide a link to your code and we can take a look.


#3

thanks Randell
link is here:

Im thinking its because my jscript is re-running the code each time a key is pressed rather than catching all inputs up until the = button is pushed.
stuggling to work it out though.


#4

Be back in a couple of hours. Need to eat some dinner.


#5

all good Randell… worked it out finally!! i needed to setup document.ready then my variables then my button click event handler.
It was re-running the function because I had the button handler at the top.

thanks so much for your help. This is tough but fun!