简体   繁体   中英

If/else statements not functioning

I cannot figure out why if/else statements are not executing properly. The code only runs through the if(i===1) loop but thats it. 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:

    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/

You also needed to change abs. to Math.abs

The abs() method does not exist. You're dealing with all integers so you can probably just remove it or fully reference it 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.

Try this

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