Calculator test 11

I cannot for the life of me get test 11 to pass.

When the decimal element is clicked, a “.” should append to the currently displayed value; two “.” in one number should not be accepted

My calculator does this yet it still does not pass is it because i use eval?

Any help would be much appreciated.

It’s not passing cuz I can hit 2 decimal points and 2 points will appear
You just want one to appear.

A user should not be able to enter the following in any calculator.


1 Like

Yeah that :point_up:

Oh and don’t forget about this case:

See this is what i thought when i was going to originally post. And when i make it so that if the last input was a “.” then it won’t let you put another “.” yet it still fails.

Right now i have it throw an error message and not execute for that case since it wasn’t part of the tests.

I would have to see what code you implemented to see what the issue is.

You can throw the error, but the tests expect you to have it function like a real calculator, so if somebody did already enter a ., it [edit:] would not let them add another.

Not so much fail, but it just will not allow the user to enter it that way.

Right, caught my mistake and fixed right as you responded.

I added this to my switch and I have redeployed the site with it added

case ".":
          const lastInput = text.slice(-1);
          if (lastInput === ".") {
              displayText: text,
              input: text
          } else {
              displayText: text + btn,
              input: text + btn

it’s not so much the last input. If they entered a decimal point 2 digits ago, would this still work?

That code will catch the case of 6…2 but it will not catch the case of

Think about how you could use indexOf or a regular expression could test if a . was already entered.

The regular expression would be best, because you should still be able to enter 6.2+3.4

Ya I just noticed that I guess I am taking the test case too exact

After getting some sleep I managed to get it to work thanks for the help