简体   繁体   中英

Checking a Calculator display has one number in the innerHTML string

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM