Stuck in test 13 in JavaScript Calculator project

Hi, campers :wave:

I almost completed the calculator project and only one test is remaining before I can get the front end certificate. The test follows:

13. If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.

I understand the user requirement and have tried to build a feature so that when I enter 5 * - + 5 , it will return 10. However, this will means that I won’t be able to perform 5 * - 5 = -25. And if I remove class="operator" from the subtract element, I will be able to enter 5 * - 5 = -25 but the 5 * - + 5 = 10 will fail. And if I restore the class operator to the subtract, I can do 10 but again -25 will fail.

How am I suppose to solve this issue?

Codepen: https://codepen.io/mohammedasker/pen/BapqoLR?editors=0010

1 Like

The problem I am dealing with has more to do with logic than implementation. The example project shown from FFC uses React and I don’t understand how it works since it’s using a class component (I am using jQuery for this project).

My question is how can I add negative numbers while still making sure not to insert multiple minuses?

1 Like

Still stuck in the challenge. While I am waiting responses, here’s my thought on how to solve the issue:

  • When I press 5, it will show 5: // 5
  • When I press *, it will add * next to 5: // 5 *
  • When I press -, it will add - next to *: // 5 * -
  • When I press +, it will remove - and *, replacing it with +: // 5 +
  • When I press 5, it will add 5 next to +: // 5 + 5
  • When I press =, it will return 10: // 10

In short, if I don’t press + button, I can add negative numbers which means I can perform 5 * - 5 = 25, possibly passing the tests.

I know there’s a hole in my logic, but this is just a start.

1 Like

I replaced the way your array of operators with two objects:

const allOps = {
  '+': true,
  '-': true,
  '*': true,
  '/': true,
  };

  const genOps = {
  '+': true,
  '/': true,
  '*': true,
  }

I took this approach as the subtraction sign just requires its own set of rules and I treated it as such. I minimally refactored you code and got it to pass. Instead of using includes I simply checked the object to see if a specific value was true (there or not).

I used the allOps for the minus exceptions and I used the genOps to simply replace your includes, but out side of that the logic is mostly the same for your original if statements .

I would recommend not using eval:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

I tried to keep my code as close to yours without changing much, but as a consequence it is may seem a bit forced as I only took in immediately relevant parts of your project.

2 Likes

Hold up, can you explain to me in details on how you approach works and possibly give an sample example? (a pseudocode will be fine)

I do not get how you managed to pass test by just making few changes and replacing includes with == since you said you simply checked if the object to see if a specific value was true. Are you suggesting that includes is not needed in the if statement and that using == is enough,

Because you said you made few changes to my code, I suspect I am making my code much more complicated than necessary.

Here’s a new change I made although it’s not doing anything:

  if (operators == genOps && operators.includes(currentResult.charAt(currentResult.length - 1))) {
    resultElement.value = currentResult.substring(0, currentResult.length - 1);
  } else if (operators == allOps && operators.includes(currentResult.charAt(currentResult.length - 1))) {
    resultElement.value = currentResult.substring(0, currentResult.length - 1);
  }

I like to use === strict equality, and that is what I used. You pretty much know where the operator element is going to be, and that place is either the last or second to last element of the string, and you know this because you always go to check the string on a change to it so with the right logic they can never put in an improper operator.

const allOps = {
  '+': true,
  '-': true,
  '*': true,
  '/': true,
  };

const str = '1+2+'

console.log(allOPs[str[str.length - 1]]) //true
1 Like

Ok, I will try to implement your approach and see if this will pass the tests.

On a side note, I managed to create a way to produce -25 and 10 at the same time. Problem is, this will still not pass the test I since I sort of broke the calculator rules.

  if(operators.includes(value) && operators.includes(currentResult.charAt(currentResult.length - 2))) {
    resultElement.value = currentResult.substring(0, currentResult.length - 2);
  }