Javascript Calculator - How to handle test #9? (hitting operators in any order)

Hey guys, so I’ve managed to make the calculator do the calculation when I press the equals button. Everything works fine up to that point. But I’m curious as to how you guys handle entering multiple operators using the immediate logic method. So basically, is it like, they enter a number like 9, press plus, then enter another number like 10, then when they press plus again, it should calculate and return the sum up until that point? (eg, 9 + 10 + returns 19).

How are you guys tracking how many operators there are? Do I even need to track how many were entered if I’m doing the immediate result logic?

Here’s my codepen for reference.
JavaScript 计算器 (codepen.io)

I don’t think there is any requirement to do that. In fact, I just looked at the example FCC provides and it does not do this. It doesn’t calculate a value until the equals key is clicked.

I see… so basically the solution is to take a long string, then figure out which is on the left and right side of the operator for each pair? (eg, string: 3 * 9 / 2,
first pair 3 * 9, that results to 27, which goes to the next pair 27 / 2)

(edit: nvm I did some editing and now it can chain combinations of operators somewhat.)

(edit 2: nvm I fooled myself. I can’t chain combinations of operators. do I need to refactor? lol)

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