Calculator test 11

Calculator test 11
0

#1

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?
Repo
Calculator

Any help would be much appreciated.


#2

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


#3

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

image


#4

Yeah that :point_up:


#5

Oh and don’t forget about this case:
image


#6

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.


#7

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


#8

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


#9

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.


#10

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


#11

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


#12

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

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

#13

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


#14

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

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


#15

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


#16

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