Calculator itself

var buttons = document.querySelectorAll('.btn'); var screen = document.querySelector('.screen'); console.log(screen.innerHTML); for (var i = 0; i < buttons.length; i++) { var btnElem = buttons[i]; btnElem.onclick = function() { var active = this.innerText; if (active <= 10 && !undefined) { screen.innerText = active; } else if (active == 'CE' || 'AC') { screen.innerText = '0'; } else { console.log('Code Error'); } } } 
 <div class="container"> <div class="calc"> <div class="top-part"> <div class="screen">This is my screen</div> </div> <div class="panel-part"> <div class="keys"> <button class="btn clear">AC</button> <button class="btn clear">CE</button> <button class="btn operator">%</button> <button class="btn operator">/</button> <button class="btn">7</button> <button class="btn">8</button> <button class="btn">9</button> <button class="btn operator">X</button> <button class="btn">4</button> <button class="btn">5</button> <button class="btn">6</button> <button class="btn operator">-</button> <button class="btn">1</button> <button class="btn">2</button> <button class="btn">3</button> <button class="btn large-btn">+</button> <div class="empty"></div> <button class="btn">0</button> <button class="btn">.</button> <button class="btn result">=</button> </div> </div> </div> 

I don’t understand how to make my variables save and then use them for calculations. I know that a very stupid question, but still. I wish that if you type 5 and 1 on the screen was 51, and not overwritten. Thank!

    1 answer 1

    Good day!

    Instead of rewriting the value on the screen:

     screen.innerText = active; 

    Just add it to the current value:

     screen.innerText = screen.innerText+active; 

    But you have to embed a check so that the starting value of "This is my screen" is erased, and you will also need to check for something that you are not adding to zero.

    I also recommend making a hidden element (with CSS display: none;) example:

     <span id="memory" style="display:none;"></span> 

    Which will be used to memorize dialed numbers after pressing the signs of arithmetic operations (+ - * / =).

    • Thank you for taking the right path :) - NotControlledMind