My calculator works well and 13th test doesn’t make sense. Change my mind

/s
:joy:
Seriously, any suggestion to complete this challenge?

change your checks to accept last operator given. At this moment doing something like 5, *, +, 5, your calculator rejects the second operator “+”, and does 5*5. But they want you to instead always accept the last operator as the one used to perform the operation, so 5+5.

The added catch is that if your second operand has a negative sign, it should be also removed. From the testcase of #13:

“5 * - + 5” = should produce an output of “10” : expected ‘-25’ to equal ‘10’

we can see that the last “+” not only overrode the original “*”, it removed the “-” as well. So as you type this sequence into your calculator you should see the following:

5

5*

5*-

5+

5+5

Hope that helps.

yes I understood the meaning, the joke was because I understood it late

what strategy should i use now? an hook like const [operator, setOperator] = useState('');?

ok thanks everyone for the help, I’m a fool, the solution was simple

now I would appreciate some advice to improve readability

switch (e.target.value) {

      case '+':
        if (lastChar === '-') {
          console.log(lastChar.slice(-2));
          lastChar = lastChar.slice(-2).replace(lastChar, '+');
          setInput(input.substr(0, input.length - 2) + lastChar);
        } else if (lastChar === '*' || lastChar === '/' || lastChar === '.') {

          lastChar = lastChar.slice(-1).replace(lastChar, '+');
          setInput(input.substr(0, input.length - 1) + lastChar);
          
        } else if (currentValue !== lastChar) {
          setInput(input + e.target.value);
        }
      return;

      case '*':
        if (lastChar === '-') {
          lastChar = lastChar.slice(-2).replace(lastChar, '*');
          setInput(input.substr(0, input.length - 1) + lastChar);
        } else if (lastChar === '+' || lastChar === '/' || lastChar === '.') {
          lastChar = lastChar.slice(-1).replace(lastChar, '*');
          setInput(input.substr(0, input.length - 1) + lastChar);
          
        } else if (currentValue !== lastChar) {
          setInput(input + e.target.value);
        }
      return;

      case '/':
        if (lastChar === '-') {
          lastChar = lastChar.slice(-2).replace(lastChar, '/');
          setInput(input.substr(0, input.length - 1) + lastChar);
        } else if (lastChar === '+' || lastChar === '*' || lastChar === '.') {
          lastChar = lastChar.slice(-1).replace(lastChar, '/');
          setInput(input.substr(0, input.length - 1) + lastChar);
          
        } else if (currentValue !== lastChar) {
          setInput(input + e.target.value);
        }
      return;

      case '-':
        if (lastChar === '.') {
          return;
        } else if (currentValue !== lastChar) {
          return setInput(input + e.target.value);
        }
      return;

      default:
      return;
    }

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