简体   繁体   中英

Increment and decrement not working

I'm trying to make the "good" button +1 point and make the "wrong" button to -1 point. Now It doesn't do any of that.

 <script> var score = 0; function scoreplus() { score = score++; } function scoremin() { if (score > 0) { score = score--; } else { score = 0; } } </script> <div class="points"> <p id="punten"> <script> document.write(score); </script> </p> <button class="" id="win" onclick="scoreplus();">GOOD</button> <button class=" " id="lose" onclick="scoremin();">WRONG</button> </div> 

Do you see what I did wrong?

You run document.write() only one time. The clicks don't affect it. Use innerHTML instead and run the <script> after the HTML.

score = score++ is not a correct assignment. score++ is enough.

 <div class="points"> <p id="punten"></p> <button class="" id="win" onclick="scoreplus();">GOOD</button> <button class=" " id="lose" onclick="scoremin();">WRONG</button> </div> <script> var punten = document.getElementById("punten"); var score = 0; function scoreplus() { score++; punten.innerHTML = score; } function scoremin() { if (score > 0) { score--; } else { score = 0; } punten.innerHTML = score; } punten.innerHTML = score; </script> 

well this score = score++ should be changed to score++ or score = score+1

also your score value will not change inside <p> tag on button clicks for now.

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