Take the total make it equal to the. total -= (storedNumber - secondStored)
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…
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.