Need help implementing a subtraction function in Calculator

Ideally the code would work something like this:

minus:

  1. Click a number have it stored (lets say 9)
  2. Retain first stored number.
  3. Click minus button (trigger event)
  4. ----- getting lost somewhere in here
  5. Pick a second number have it stored; (lets say 6)
  6. Take the total make it equal to the. total -= (storedNumber - secondStored)
  7. calcDisplay.innerhtml = total (display 3)

Please let me know where I’m going wrong, and how would I store two numbers? Or is it necessary to store two numbers? There is probably a much cleaner solution I’m not yet seeing.

    minus.addEventListener("click", function(){
        // store it same way as first one?
        for(let index = 0; index < numbers.length; index++)
        {
            numbers[index].addEventListener("click", function(){
                let secondStored = 0;
                secondStored = numbers[index].innerHTML;
            })
        }

Actually you do not need to store the second number and then evaluate expressions. I will just give a hint right now… Checkout the eval function in javascript. If you just build the string something like “2+9-4*3/1” )and many more numbers and operations too…) and then pass it to eval method. It returns the result, so that you do not need to store the numbers and process explicitly…
Hope this helps… :slight_smile:

1 Like

Thanks going to see if I can figure this out.

1 Like

I gave myself an hour to try and figure this out, but unfortunately I haven’t succeeded. My brain seems stuck on trying to store two numbers, and cannot think of how I would evaluate this without them. My mind is not yet open to all the different problem solving avenues of Javascript. I thought was on to something with this.

    minus.addEventListener("click", function(){
    
        for(let index = 0; index <  numbers.length; index++)
        {
            total = eval(storedNumber - numbers[index].innerHTML);
            console.log(total);
        }
  
    })

but I quickly hit a wall again. I would really love to know how to solve this.

I would recommend using a base string, just to store the expression till now. Just keep pushing any valid number or operation the user entered to the string. When the user presses equals(=), perform eval(baseString) to get the answer.
Also, be sure to push only valid characters to the baseString. Like doing 2+3-3, should keep pushing in the following order: 2, then user enters + , then 3, then -, then 3. Finally when user clickes equals, perform the eval to get the answer, and print it.
Some corner cases to consider while using eval:

  • Leading 0s are harmful (give errors). Remove them before passing to eval method
  • Multiple operations occuring consecutively results in wrong answers. Make sure to have only one operation sign between 2 numbers, or extra minus sign for negative numbers.
  • Multiple decimal places in the same number can be ambiguous. Check if the number already has decimal point before adding one whenever the user enters decimal point.
1 Like

I like your answer but Ill be honest I’m not fully able to understand it and wouldn’t know how to implement into my code. Thank you though!

Would still appreciate some help on this one.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.