Javascript Calculator Exercise- stuck


#1

Hi everyone,

I’m stuck on the javascript calculator project.

Not fully completed, and design, is still to be done, but I’ve mostly got it working, where it takes into account precedence of operators and what not. However, it stops whenever I try to enter an equation which ends in brackets like “55 * (44 - 33) =”, the script stops working. Console.log displays “Error: Script terminated by timeout at: operate@https://fffear.github.io/javascript-calculator/jscript.js:71:14@https://fffear.github.io/javascript-calculator/jscript.js:212:5”.

I looked at lines 71 and 212 in my jscript.js file, but I don’t know why it’s doing this.

Can someone please provide any assistance? It would be much appreciated.


https://fffear.github.io/javascript-calculator/


#2

Sounds like you may have an infinite loop, hence the ‘timeout’.

I’m not sure, but I suspect the while (inputs[inputs.length - 1] != "(") , at line 71, might be causing this. I haven’t taken the time to understand all the variable names you use, and how you use them (‘tokens’, etc.), but my money is on this ‘while’ statement never encountering a ‘(’ once it is triggered in certain cases (e.g. when processing an input containing parentheses, or processing part of an input that did contain parentheses), and thus never stops looping and never exits that loop. For example, if it is called after the ‘(’ in your example expression has been removed from the inputs var, then it will never stop.

Inserting some console.log(var_in_question) statements just before that while loop to see the state of the involved vars, and maybe also inside the while loop to see what, if anything, changes each loop, might be a good first troubleshooting step.


#3

Hi @jinjagit,

Thanks for that, I sorted out the bug.

Yes there was an issue with the while loop.

Additionally, I didn’t declare the “i” variable with let in the for loop when tokens[i] === “=”, which caused the code to do weird things.

I can’t believe it took about 2 days to sort out a bug, but I just kept getting stuck, and had to go back and keep changing my code. How long does it take you normally to debug and get unstuck?


#4

Great news, and well done!

It takes me much less time to find the same type of bug, when it reoccurs. And, yes, I have taken days to figure out bugs before. I think what’s really tough when you are just starting out is that it seems like everything is hard, whereas after some time learning and coding you can take more knowledge and ability for granted (but will still meet / make new bugs).

In fact, when it is really hard / confusing is when I am really learning (if I ever find the bug(s)), as this is when I really make breakthroughs in my understanding and abilities with code. Just as you have done! :slight_smile: Also, with repeated debuggings, your debug skills and senses will improve.

Congrats, and keeping squashing those bugs.