JS Calculator trouble with RegExp and operators "+-*/."

JS Calculator trouble with RegExp and operators "+-*/."
0

#1

Hi guys,

I’m trying to check if a new input contains an operator i.e. “±*/.”.
If yes, it checks if the last character of a string already has an operator so that it may be replaced with the new operator.
If not, it simply updates the string with the new operator.
In the end the whole string will be eval(str) to get the result. That’s the idea.

Here’s a snippet…

var operators = /[±x/.]/;
//------------------------PROBLEM-----------------------
else if (input.match(operators)) {
if (str.endsWith(operators)) { return str.slice(0, -1) + input; }
else { str += input; }
}
//----------------------------------------------------------------

I cannot find the problem in the above code. Any help would be great. Here’s a link to the code. The problem area is commented.

Thank you
Suraj


#2

I’m not sure if this is the issue but:

  • in the code above, you’ve used the ± operator. I assume that’s a typo, but if you are trying to match on it you can’t then eval on it.
  • in the codepen you are trying to match on the letter x rather than *, you cant eval on that either - 1 x 2 is just nonsense.

#3

yes ± is only a typo here.
You’re right about changing the “x” to “*”. It is an error but it doesn’t solve the problem.
Have to keep searching. Just can’t figure out the problem.
Thanks though.


#4
  • You’ve broken the regex - it can just be /[±*/.]/, though you don’t really need regex.
  • if (str.endsWith(operators)). What you’re trying to do here is (if I use a working regex) if (str.endsWith(/[+-*/.]/)). The string never ends with that - the method checks whether a string ends with a specific character or set of characters, you can’t use a regex to build that set of characters - it has to be a string you check for.
  • the variable chk doesn’t exist - when you test for =, you then go into seeing if chk is equal to an operator, but it never is because chk doesn’t exist.

Something like (NOTE this is incomplete and won’t work properly, but it does let you input values, and I’m not using regex, and I’ve ignored logic for decimal points, which I think you need to work on anyway):

var str = ""; //stores the string of inputs
var operators = ["+", "-", "*", "/"];

function endsWithOperator(str) {
  operators.includes(str[str.length - 1]);
}

//function to process inputs/clicks
function getValue(input) {
  //if clr
  if (input === "clr") {
    str = "";
  } else if (input === "del") {
    str.slice(0, -1);
  } else if (operators.includes(input)) {
    if (endsWithOperator(str)) {
      str = str.slice(0, -1) + input;
    } else {
      str += input;
    }
  } else if (input === "=") {
    // FIXME do the `chk thing`, not just pass false
    if (false) {
      str = str.slice(0, -1);
    } else {
      str = eval(str);
    }
  } else {
    str += input;
  }
}

//button click function
$("button").click(function() {
  getValue(this.id);
  $("#display").html(str);
});

#5

Hey Dan,
I used the long way in the meantime. It works. Have a look. Still have to sort out decimals properly.
But i will use your operators array and “includes” method to simplify stuff now.
Thank you for taking the time.