2

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 .

2
  • you must ensure point is treated as a number, points = parseInt(points )+ 1; Commented Jul 7, 2014 at 12:43
  • That, and don't reset the point value on each click. Commented Jul 7, 2014 at 12:44

3 Answers 3

4

You should declare your var points outside click function. E.g :

<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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

Declare var points outside function:

var points = 0;

function onClick(){
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.