Javascript Calculator - Not passing 13,14,15

Hey guys so I took someone’s advice and basically refactored my calculator, with the objective of trying to keep it as simple and lightweight as possible. I created a calculator that passed all the tests with the exception of Test #15, aka the one dealing with negative numbers. So I created some code to deal with that, and now if I do the tests individually, it works. Eg, 3 + 5 * 6 - 2 / 4 equals 32.5, 2/7 equals 0.28…, and I can make numbers into negative numbers. I originally thought to do some kind of regex and only format it right before it calculates, but I felt that would be too difficult and complex for me. I wouldn’t even know where to begin honestly. So I came up with a solution where I have a variable that checks if something is a negative number.
Here’s the code.

let negativeNumCheck = /.*[\+\/\*\-]$/.test(input)
    console.log(negativeNumCheck)
    
    if(isNegativeNumber && inputtedOperator != "="){
      inputtedOperator = ")" + inputtedOperator
      isNegativeNumber = false
    }else if(isNegativeNumber && inputtedOperator == "="){
      setInput(prevVal => prevVal + ')')
      setActivateCalculate(true)
    }
    
    if (inputtedOperator != "=" && inputtedOperator != "-"){
      setInput(prevVal => prevVal + inputtedOperator)
    }else if(negativeNumCheck && inputtedOperator == "-"){
      setInput(prevVal => prevVal + "(" + inputtedOperator)
      isNegativeNumber = true
    }else if(negativeNumCheck == false && inputtedOperator == "-"){
      setInput(prevVal => prevVal + inputtedOperator)
    }else if(inputtedOperator === "=" && activateCalculate == false){
      setActivateCalculate(true)
    }

tl;dr, it checks if the last operator entered at that moment was an operator. if so, and if the current operator pressed is a minus sign, then put a parentheses with a minus sign. the user then can enter how many numbers he wants. if he presses an operator again, then add the closing sign before entering whatever operator he pressed. now negative numbers can be successfully calculated.

everything was working up until the negative number solution. then I added logic to fix that. if there’s something I’m missing or overlooking please let me know. and if I have to completely rewrite my calculating solution then please give me some tips?? thx so much for taking the time to read this.

Here’s my codepen

When I run the tests and look in the browser console (which should always be a first stop) I see this:

When I look at line 50 (your lines may be different as I’ve reformatted, I see this:

  function calculate(){ 
    let result = eval(input)
    console.log(result)
    setInput(result)
    setActivateCalculate(false)
  }

All that looks pretty normal but I want to see what eval is seeing so I add this:

console.log('input =', input)

on the first line of that function. This is the output from the tests:

input = 51+5+92
input = 3+5
6-2/4
input = 5-9+5
input = 10.5-5.5
input = 55.5
input = 10.5+5.5
input = 10/2.5
input = 5
(-5)
input = 5*(-)+5

So, it is no crashing for the first 8 there, and crashing on the last. That kind of makes sense to me because when I drop that into the console I get:

Screen Shot 2022-03-18 at 10.00.06 AM

In other words, JS does not know how to parse that string.

1 Like

I see… ok thats cool, so I just need to add some more logic to compensate for that… Thank you!

1 Like

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