title: How to make equal sign do repetitive calculation in JavaScript?
body: <p>Brief Description: I am trying to make a basic calculator using html and JavaScript. So far I have successfully done all the basic functionality of a calculator with the number, operators, decimal, and equal sign keys (button).</p>
  
  <p>Problem: I am trying to make it so by pressing the equal sign multiple times after an operation has completed it will do the last operation. Such as the example follows:</p>
  
  <p>Operation: 5+6-3</p>
  
  <p>Answer(equal sign pressed once): 8</p>
  
  <p>Equal sign pressed twice: 8-3 = 5 (this does not occur in my code and cant seem to find out why)</p>
  
  <p>The last operation in this case would be "-3" </p>
  
  <p>The code is below, I took our most of the functions that and bits of code that does not cause the issue as the code I have is about 200 lines. (the code below is about 80 lines)</p>
  
  <p>Code:</p>
  
  <pre><code>var keys = document.querySelectorAll('#calculator span');
  var operators = ['+', '-', 'x', '÷'];
  var input;
  var inputValue;
  var buttonValue;
  var equalOnce = false;
  var decimalAdd = false;
  var lastOperation;
  
  readIn(keys, input, inputValue,buttonValue);
  
  function readIn(keys, input, inputValue, key) {
    for(var i = 0; i &lt; keys.length; i++) {
      keys[i].onclick = function(e) {
        input = document.querySelector('.screen');
        inputValue = input.innerHTML;
        key = this.innerHTML;
        keyPressed(key, inputValue, input);
      }
    }
  }
  
  //clears screen and input
  function clearScreen(input){}
  
   //calculates all math functions within the equation.
  function evalulated(lastCharacter, equations, operator, input){
        //replaces x with * and ÷ with /
        equations = equations.replace(/x/g, '*').replace(/÷/g, '/');
        //checks last character and removes if its an decimal or operator
        if(operator.indexOf(lastCharacter) &gt; -1 || lastCharacter == '.')
          equations = equations.replace(/.$/, '');
        if(equations){
          input.innerHTML = eval(equations);
        }
        return;
      }
  
  //finds the last operator and take the operator and the last digits/number used with the operrator and save it into lastOperation
  function findLastEquation(equation, lastOperation){
    var plusLastIndex = equation.lastIndexOf('+');
    var subLastIndex = equation.lastIndexOf('-');
    var divideLastIndex = equation.lastIndexOf('÷');
    var multipleLastIndex = equation.lastIndexOf('x');
  
    if(plusLastIndex &gt; subLastIndex &amp;&amp; plusLastIndex &gt; divideLastIndex  &amp;&amp; plusLastIndex &gt; multipleLastIndex){
      lastOperation = equation.slice(plusLastIndex, equation.length);
    }else if(subLastIndex &gt; plusLastIndex &amp;&amp; subLastIndex &gt; divideLastIndex  &amp;&amp; subLastIndex &gt; multipleLastIndex){
      lastOperation = equation.slice(subLastIndex, equation.length);
    } else if(divideLastIndex &gt; plusLastIndex &amp;&amp; divideLastIndex &gt; subLastIndex  &amp;&amp; divideLastIndex &gt; multipleLastIndex){
      lastOperation = equation.slice(divideLastIndex, equation.length);
    } else if(multipleLastIndex &gt; plusLastIndex &amp;&amp; multipleLastIndex &gt; subLastIndex  &amp;&amp; multipleLastIndex &gt; divideLastIndex){
      lastOperation = equation.slice(multipleLastIndex, equation.length);
    }else{
      lastOperation = 0;
    }
    return lastOperation;
  }
  
  function keyPressed(key, inputValue, input) {
    var equation;
    var lastChar;
    var newEquation;
  
    if(key == 'C') {
      clearScreen(input);
    }
    else if(key == '=') {
      if(equalOnce == false) {
        equalOnce = true;
        equation = inputValue;
        lastOperation = findLastEquation(equation, lastOperation);
      }else {
        equation = equation.concat(lastOperation);
        alert(equation);
      }
      lastChar = equation[equation.length - 1];
      evalulated(lastChar, equation, operators, input);
  
      decimalAdd = false;
    }
    else if(operators.indexOf(key) &gt; -1) {
      lastChar = inputValue[inputValue.length - 1]; 
      if(operators.indexOf(lastChar) &gt; -1 &amp;&amp; inputValue.length &gt; 1) {}
      equalOnce = false;
    }
    //adds a decimal to input, adds a '0.' if no integer was before it
    else if(key == '.' &amp;&amp; equalOnce == false) {
      if(decimalAdd == false) {}
        input.innerHTML += key;
        decimalAdd = true;
      }
      equalOnce = false;
    }
    else {          
      if(equalOnce == true){
        clearScreen(input);
      }
      if(key == '.'){}
      input.innerHTML += key;
    }
  </code></pre>
  
  <p>}</p>
  
  <p>Any help or suggestion will be great! Thank you in advance!</p>