Calculator Test: 5*-+5 test

I have been stuck on the last test requirement for the calculator project. I intended to use a similar approach to how a phone calculator would just disregard the latest operator inputs if it was followed by a non minus operator. However, the test will not pass even though I believe it to do the same thing. Here is the part of my code for this test

  if (
      (symbol === "*" || symbol === "+" || symbol === "/") &&
      (topDisplay.charAt(topDisplay.length - 2) === "*" ||
        topDisplay.charAt(topDisplay.length - 2) === "+" ||
        topDisplay.charAt(topDisplay.length - 2) === "/" ||
        topDisplay.charAt(topDisplay.length - 2) === "-")
    ) {
      setTopDisplay(memory[0] + symbol);
    }

This prevents the user from typing in any more operators after two have been entered (another part of my code restricts things like ** , //, */, *+, etc)

Am I close to the right approach for this specific test? I feel it essentially does the job but I guess it wants to wait until calculation to disregard the previous operators. Any advice is welcomed, thanks in advance

Also after looking at it, I realize my solution would have issues if someone were to chain together equations. But, I just want to pass the simple 5*-+5 test.

I think we need to see all of your code before we can help figure out why you are having problems. The best would be for you to put it somewhere public and then give us a link.

1 Like

Will do. Here is a link to my repo

I’m getting two tests that fail right now, test 9 and test 13.

Try the following:

1 + 2 + 3 + 4 + 5

Notice that when you enter the second plus sign, the number after the first plus sign disappears completely and you are just left with 1 +. And this pattern continues with each additional plus sign. Instead of keeping a running total, your calculator is just throwing away values every time you add a plus sign. This is why test 9 is failing.

I’m not sure why test 13 is failing. The tests say the expected answer is 10 and that’s what I get when I enter 5 * - + 5.

1 Like

thank you for your reply. Test 9 worked before I started focusing my code to pass 13. I think I can get 9 back to working, but 13 is what is stumping me. I am also not sure why. I have heard that using React 18 isn’t always supported for the FCC tests, which is what I am using. I will continue to search for 13’s solution!

Use the old render method or downgrade to make sure it isn’t a test issue.

https://github.com/freeCodeCamp/freeCodeCamp/issues/45922

1 Like

thank you for your reply. Once I downgraded to react 17.0.2, my current code passed the tests.

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