简体   繁体   English

如果/其他语句不起作用

[英]If/else statements not functioning

I cannot figure out why if/else statements are not executing properly. 我无法弄清楚为什么if / else语句无法正确执行。 The code only runs through the if(i===1) loop but thats it. 该代码仅通过if(i === 1)循环运行,仅此而已。 I'm doing a simple exercise wherein I check the number submitted and see if the users gets hotter or colder to a preset number. 我正在做一个简单的练习,其中我检查提交的数字,并查看用户是变热还是变冷到预设数字。

<html>
<head>
    <title>Test</title>
    <script src="jquery-1.11.0.min.js"></script>
    <script>
    $(document).ready(function () {
        var checkNum = Math.floor(Math.random() * 10);
        alert("Random number is" + checkNum);
        var i = 0;
        var scopedVal;
        var submitVal;
        var hotCheck = function (submitVal) {
            if(i === 1) {
                alert("try again");
                scopedVal = submitVal;
                alert("scopedVal is" + scopedVal);
            } else {
                if(abs(scopedVal - checkNum) > abs(submitVal - checkNum)) {
                    alert("Hotter");
                    scopedVal = submitVal;
                } else {
                    alert("colder");
                    scopedVal = submitVal;
                }
            }
        };
        $('.subm').on('click', function () {
            i++;
            alert(" i is" + i);
            alert("button pressed");
            submitVal = $(this).closest('#outsideContainer').find('.num').val();
            if(submitVal === checkNum) {
                alert("You got it");
            } else {
                hotCheck(submitVal);
            }
        });
    });
    </script>
    <style>
    body {
        width: 900px;
        color: white;
        margin: auto;
    }
    #outsideContainer {
        width: 400px;
        color: grey;
        margin: auto;
        position: relative;
    }
    </style>
</head>
<body>
    <div id="outsideContainer">
        <form>
            <input type="text" name="text" class="num">
            <br/>
        </form>
        <div id="buttonSubm">
            <button class="subm">Submit</button>
            <div></div>
</body>
</html>

I'm assuming this is a simple fix and it is driving me nuts. 我假设这是一个简单的解决方法,并且让我发疯。

If you want to run the "else" code as well as the "if" code remove the else statement: 如果要运行“ else”代码以及“ if”代码,请删除else语句:

    var hotCheck = function(submitVal){
        if(i===1){ 
            console.log("try again");
            scopedVal = submitVal;
            console.log("scopedVal is " + scopedVal);
        };
        if(Math.abs(scopedVal-checkNum)> Math.abs(submitVal - checkNum)){
            console.log("Hotter");
            scopedVal = submitVal;
        } else {
            console.log("colder");
            scopedVal = submitVal;
        };
    };

Demo: http://jsfiddle.net/hHC88/ 演示: http//jsfiddle.net/hHC88/

You also needed to change abs. 您还需要更改吸收。 to Math.abs 到Math.abs

The abs() method does not exist. abs()方法不存在。 You're dealing with all integers so you can probably just remove it or fully reference it Math.abs() 您正在处理所有整数,因此您可以将其删除或完全引用Math.abs()

You have to replace the 您必须更换

if( abs(scopedVal-checkNum)> abs(submitVal - checkNum)){ 

to

if( Math.abs(scopedVal-checkNum) > Math.abs(submitVal - checkNum)){

abs() is a method to Math. abs()是Math的一种方法。

Try this 试试这个

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

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