为什么它总是返回'foo'?

我决定创建一个小程序,随机给你2个数字和一个符号(-或+),如果用户回答正确,它应该是 console.log('hey'); 并显示新的2个数字和一个符号。现在,我的应用程序只显示两个数字,并总是说 console.log('foo'); 即使用户的答案是正确的。这是很容易的,但我不明白为什么也许我错过了什么或忘记了,请帮助!我决定创建一个小程序,随机给你2个数字和一个符号(-或+),如果用户回答正确,它应该console.log(‘hey’); 并显示新的应用程序。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Thinking...</title>
</head>
<body>
    <div>
        <span id="a">A</span> <span id="sign"></span> <span id="b">B</span> =
        <label for="result"></label><input type="number" id="result"> <button type="button" id="btn" onclick="checkResult()">press</button>
    </div>

    <script>
        let numberA = document.getElementById('a'),
            numberB = document.getElementById('b'),
            sign = document.getElementById('sign'),
            input = document.getElementById('result');

        function checkResult(a, b, sign, result) {

            if (sign === '-') {
                return a - b;
            } else if (sign === '+') {
                return a + b;
            }

            if (result === a-b || result === a+b) {
                console.log('hey');
            } else if (result !== a-b || result !== a+b) {
                console.log('foo');
            }

        }

        function getRandomSign() {
            let signs = ['-', '+'];
            return signs[Math.floor(Math.random()*signs.length)];
        }

        function getRandomNumber(a, b) {
            return Math.floor(Math.random() * (b - a + 1)) + a;
        }

        let aRandom = numberA.innerHTML = getRandomNumber(1, 10);
        let bRandom = numberB.innerHTML = getRandomNumber(1, 10);
        let signRandom = sign.innerHTML = getRandomSign();

        console.log(checkResult(aRandom, bRandom, signRandom, input.value));

    </script>
</body>
</html>

解决方案:

var numberA = document.getElementById('a'),
    numberB = document.getElementById('b'),
    sign = document.getElementById('sign'),
    input = document.getElementById('result');
    
var aRandom = numberA.innerHTML = getRandomNumber(1, 10);
var bRandom = numberB.innerHTML = getRandomNumber(1, 10);
var signRandom = sign.innerHTML = getRandomSign();

function checkResult() {
    let temp;
    if (signRandom === '-') {
        temp = aRandom - bRandom;
    } else if (signRandom === '+') {
        temp = aRandom + bRandom;
    }
    if (temp === parseInt(input.value)) {
        console.log('hey');
    } else {
        console.log('foo');
    }

}

function getRandomSign() {
    let signs = ['-', '+'];
    return signs[Math.floor(Math.random()*signs.length)];
}

function getRandomNumber(a, b) {
    return Math.floor(Math.random() * (b - a + 1)) + a;
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Thinking...</title>
</head>
<body>
    <div>
        <span id="a">A</span> <span id="sign"></span> <span id="b">B</span> =
        <label for="result"></label><input type="number" id="result"> <button type="button" id="btn" onclick="checkResult()">press</button>
    </div>
</body>
</html>

给TA打赏
共{{data.count}}人
人已打赏
解决方案

R:将as.字符与as.整数或as.数字进行比较。

2022-5-14 12:00:37

解决方案

react-bootstrap如何使用自定义表单控件组件?

2022-5-14 13:00:26

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索