Help with JS Calculator project - spoiler

Help with JS Calculator project - spoiler
0

#1

Hi fellow campers,

I’m working through the calc project using the testing suites here

but I’m unsure how I can chain my numbers - test 9.
My logic works if I do each operation separately eg 1 + 1 = 2 then 2 * 4 = etc, which gives the right answer but if I do 1 + 1 * 4 =, it doesn’t work correctly.

Can anyone offer some help/suggestions? I know I somehow need to store the intermediate values but I can’t figure it at the mo. :confused:

codepen here

note - tests only work in chrome


#2

If you’re keeping a running sum, then after 1+1 your running sum will be 2. Then *4 will give 8. However, if you’re not keeping a running sum, but storing the full expression and using a standard parser when “=” is entered, then this parser will probably assume that * takes precedence over +, so presumably you are getting 5 - ie, the equivalent of 1 + (1 * 4), which is THE standard arithmetic expression parse of 1 + 1 * 4 in every computer language I know.


#3

Thanks @sabrawer, I’ll see how I get on.