简体   繁体   中英

How can i add numbers to a variable in JS?

I have been trying to do a simple game where you try to get the right number as the random number. If you win, you get 1 point. But I wasn't able to give that point. Here is what i have done :

<html>
<head>
    <title>Luck Game</title>
    <script>
    function onClick(){
        var number = Math.floor(Math.random()*10);
        var usernumber = document.getElementById("input").value ;
        var points = 0;
        document.getElementById("Answer").innerHTML = number ;
        document.getElementById("points").innerHTML = "Points in your account : " + points;
        if(usernumber == number){
            document.getElementById("WIN?").innerHTML = "You WON !. You got 1 point added to your account !";
points = points + 1;

        }
        else{
            document.getElementById("WIN?").innerHTML = "Ow, no luck, you lost..."
        }

    }
    </script>
</head>
<body>
    <input id="input"></input>
    <p id="Answer"></p>
    <button onClick="onClick()"></button>
    <p id="WIN?"></p>
    <p id="points"></p>
</body>

But the "points = points + 1" is not working ! what is wrong ? please help me .

You should declare your var points outside click function. Eg :

<script>
var points = 0;
    function onClick(){
        var number = Math.floor(Math.random()*10);
        var usernumber = document.getElementById("input").value ;

        document.getElementById("Answer").innerHTML = number ;
        document.getElementById("points").innerHTML = "Points in your account : " + points;
        if(usernumber == number){
            document.getElementById("WIN?").innerHTML = "You WON !. You got 1 point added to your account !";
points = points + 1;

        }
        else{
            document.getElementById("WIN?").innerHTML = "Ow, no luck, you lost..."
        }

    }
    </script>

So the counter will be saved somewhere else without being resetted everytime function procs.

The way you've written it, points is a variable local to the onClick() function so it will get reset to zero each time the function is called. If you pull the initialization of points out of onClick() then you will be able to safely increment its value:

<script>
    var points = 0;
    function onClick(){
        var number = Math.floor(Math.random()*10);
        var usernumber = document.getElementById("input").value ;

        document.getElementById("Answer").innerHTML = number ;
        document.getElementById("points").innerHTML = "Points in your account : " + points;
        if(usernumber == number){
            document.getElementById("WIN?").innerHTML = "You WON !. You got 1 point added to your account !";
            points++;
        } else {
            document.getElementById("WIN?").innerHTML = "Ow, no luck, you lost..."
        }
    }
</script>

Declare var points outside function:

var points = 0;

function onClick(){
    ...
}

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