JS Calculator feedback, bugs hunting


#1

hello Campers,

I just finished my JS Calculator. I wonder if my code design approach was the right one. I feel I used to many nested ifs conditions in this project.

Project Link - https://codepen.io/tiagovalverde/pen/WENWPa

Any feedback and bug hunting is appreciated!

Thank you all!


#2

looks like it works, the = button is misaligned for me. A number runs off the display when you push the 10th digit


#3

Which browser did you got the equal button not aligned?
Thank you @moT01


#4

firefox 54.0 for ubuntu, i dont think there’s too many nested ifs btw. There may have been a better way, but it works


#5

This looks really good. It correctly allows for negative numbers i.e. you can write -5 * 4 or 5 * -4 and it will give you -20. It allows for operator chaining successfully. It also looks visually nice and thematic.

There are a few bugs/flaws you might want to look into.

First, division by zero should be treated with separate care for your calculator, and one shouldn’t just use what JavaScript does on its own. For example, 2 / 0 gives infinity, when you should display your own error message. Also, the indeterminate forms, like 0 / 0 or 2 / 0 * 0 give NaN because that’s what JavaScript does, when you really should just display something like “Error”.

Second, while you do have input overflow controls for the top row, the bottom row overflows when chaining together a bunch of operations.

There are also some logic flow issues with button presses. Take CE i.e. clear entry. For example, if I press 12 + and then press CE to get rid of the +, the top row then displays 0 rather than 12.

Also, there are some issues with the decimal point. Your calculator allows for 4 / . + and I assume you don’t want to allow an isolated decimal point by itself.

Finally, when I just have 0, I am allowed to write 0 + 4, 0 - 4, and 0 / 4, but for some reason I can’t write a * after the initial 0. You might want to look into that.

But on the whole, well done and it works quite logically. :slight_smile:


#6

Thank you very much @sgoldber61 for the feedback. I will make the corrections you mentioned. I’m thinking in starting the logic from scratch because the code it’s getting messier as I solve the bugs I found.