Calculator project!

Calculator project!
0

#1


Hello, this is my calculator project, the problem is that it chains only the last pair of values and not the others.
Can someone help me what can i do to fix it?


#2

The reason you only get the last pair of values is because of:

    if (operation == 1) {current = eval(memory) * eval(current);}
    if (operation == 2) {current = eval(memory) / eval(current);}
    if (operation == 3) {current = eval(memory) + eval(current);}
    if (operation == 4) {current = eval(memory) - eval(current);}

memory will always have the 2nd to last value clicked and current the most current value clicked. In the following:

  $('.buttonOperator').click(function (e) {
    op = e.target.id
    if (op.indexOf('*') > -1) {operation = 1} // codes for *
    if (op.indexOf('/') > -1) {operation = 2} // codes for /
    if (op.indexOf('+') > -1) {operation = 3}; // codes for +
    if (op.indexOf('-') > -1) {operation = 4}; // codes for -

    memory = current // store value
    current = ''
    $('#screen').html(current)
  })

after any operator (’*’, ‘/’, ‘+’, ‘-’) is clicked you set memory to whatever current was and then current gets ‘’. So you are wiping out the history during this part of the code.

One option is to store each value/operator into an array, so you have a history of all values and operators, then traverse the array (when ‘=’ is clicked) to make a calculation.