简体   繁体   English

检查计算器显示的 innerHTML 字符串中有一个数字

[英]Checking a Calculator display has one number in the innerHTML string

I'm working on a calculator with vanilla JavaScript.我正在使用香草 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 语句来确定当前显示的结果是否在字符串中只剩下一个数字。 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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