LeFizzy LeFizzy - 3 months ago 15
Javascript Question

Javascript increment value on click (working for one click++)

When a user clicks a

<button>
, I need to increment a value.

My script works, but when I do a second click, it's hanging on the first value it's got.

What's going wrong with it please?



var global_score;
var current_score;
function question_answer(event_sender, sent_answer) {
global_score;
current_score = document.querySelector(".game-current-score-main h2");
if(sent_answer == "increment") {
global_score++;
current_score.innerHTML = "Score:" + global_score;
}
else if(sent_answer == "decrement")
{
global_score--;
current_score.innerHTML = "Score:" + global_score;
}
};




Answer
var global_score = 0; // initialize
var current_score;
function question_answer(event_sender, sent_answer) {
    global_score; // you do not need this line
    current_score = document.querySelector(".game-current-score-main h2");
    if(sent_answer == "increment") {
        global_score++;
        current_score.innerHTML = "Score:" + global_score;
    }
    else if(sent_answer == "decrement")
    {
        global_score--;
        current_score.innerHTML = "Score:" + global_score;
    }
};