HELP! NO ENTIENDO Build a JavaScript Calculator!

help the console tell me x13. If 2 or more operators are entered consecutively, the operation performed should be the last operator
entered (excluding the negative (-) sign.

No entiendo que hacer

1 Like

So, if you type 8 / * 5, the answer should be 40ā€¦ in your calculator it answers infinity. There appears to be a bug in your code that doesnā€™t handle someone trying to change the operator midway through.

Basically test 13 is that your calculator can handle someone hitting multiple operators, meaning they can change it. So if they type 5 /, but then change their mind and want * instead, pressing * should change the operator to *ā€¦ it seems your algorith is using the 0 that gets displayed after you hit the second operator, and therefore instead of being 5 *+ 5 = 10, it becomes 0 + 5 = 5ā€¦ I did notice it works correctly if your first operator is a + or -, but breaks with * or /ā€¦ hope that helps isolate your issue.

thank you so much, i dont know yet how to put all that information in the js but i tried ok

i dont know how to solve this problem

What problem? What specifically is going on?

hi, to solve this exercise i need 16/16 and i have 15/16 and the console tell me :
x13. If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.

i understend but i dont know how to solve , i serch everywhere online and i dont know, maybye try again all the code

To implement that logic I used regex.

i dont know how to do it in that way

Hello again.

So basically youā€™re calculator has a nice simple flow, taking what has been entered, and putting it to screenā€¦ but because a person is weird and may hit buttons in orders they shouldnā€™t you need to implement some logic to check for these problems, and resolve themā€¦ take for instance, try typing this in your calculator:

3 + =

That should return threeā€¦ but instead itā€™s putting ā€˜equalsā€™ into the displayā€¦ things break shortly after thatā€¦ your program assumes after hitting the +, the user would type another number.

So there are a lot of ways to solve thisā€¦ regex, if/then statements, checking functionsā€¦ you get to decideā€¦ simply if you track the ā€œstateā€ of your calculator, you can write logic to check some thingsā€¦ what I mean by this is tracking what was previously pressedā€¦

If someone has previously pressed the + sign, and presses it again, do nothing. If someone has previously pressed the + sign, then hits the equals, ignore the previously pressed +. That kind of thingā€¦ Donā€™t do it exactly like that though because there is tons of button press combinations, but thats the kinda thing I mean. Its messy, but thats what makes it a challenge :slight_smile:

2 Likes

Then, I suggest you take some time to know about it.
Cause itā€™s really helpful and will help you to solve so many problems easily.
Or else you can go with conditionals and functions then youā€™ll be doing lots of nesting with them.

1 Like

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