I'm working on a calculator with vanilla JavaScript. I'm trying to make an if statement to find out whether the current result displayed has only one number left in the string. If this is true I want to make sure when the user clicks the delete button the current display returns to the default display instead of having no numbers in the string. I hope I explained this properly. How would I go about making this check.
const deleteNumber = () => {
let newDisplayedResult = currentResult[0].innerHTML.slice(0, -1);
if (firstNumber !== "" && currentOperator !== "") {
secondNumber = newDisplayedResult;
currentResult[0].innerHTML = newDisplayedResult;
} else {
firstNumber = newDisplayedResult;
currentResult[0].innerHTML = newDisplayedResult;
}
};
let re = new RegExp('^[0-9]$');
re.test(str)
or:
str.length === 1 && "0123456789".split("").includes(str)