I'm building a vanilla JS calculator and trying to use a regex .test() function to restrict the user from using an operator more than once in a row
i.e. 2++++3
It is structured as an if else statement, and the problem is that the regex test works on the first time, but then fails the second time thus causing the code to jump to the else statement when it shouldn't. Is my regex wrong? Am I using the wrong function to run the comparison?
var testOperands = /[+-/*=]/g;
switch (x) {
case "+":
if(testOperands.test(currentEntry[0])){
currentArea.textcontent = x;
totalArea.textContent = totalArea.textContent + "";
} else {
currentArea.textContent = x;
totalArea.textContent = (totalArea.textContent + x);
currentEntry = ["+"];
totalEntry.push(x);
}
break;
case "-":
if(currentEntry[0] === "-"){
currentArea.textcontent = x;
} else {
currentArea.textContent = x;
totalArea.textContent = (totalArea.textContent + x);
currentEntry = ["-"];
totalEntry.push(x);
}
break;
here is the full code: https://codepen.io/brianofblades/pen/KQWVYN