0

this might be a dumb thing to ask but I am getting a lot of trouble with this for and quiz I need to make for and assignment, and I am trying to increment the score when the user clicks to the correct button. However, the score is not incrementing. here is a little sample of what it looks like.

<!DOCTYPE html>
<html>

<head>
	<meta charset = "utf-8">
	<title>Increment Button</title>
</head>

<body>

<button onclick="IncrementScore()"> Increment</button>

<script>
	var score = 0;
	
	function IncrementScore()
	{
		score++;
	}
	
	console.log(score);
</script>
</body>


</html>

6
  • 1
    have your tried score += 1? Commented Nov 8, 2019 at 18:03
  • 3
    Why are you setting score back to 1 after you increment it? Commented Nov 8, 2019 at 18:05
  • yes, I have and it's not working either. Commented Nov 8, 2019 at 18:05
  • console.log(score) should be inside the function. Commented Nov 8, 2019 at 18:05
  • Was score = 1 causing the problem, or was that a typo of some sort? If the former, you shouldn't remove it from the question. Commented Nov 8, 2019 at 18:07

4 Answers 4

1

You have a few issues.

  1. You probably want to console.log from within the IncrementScore function.
  2. You want to increment the variable using += 1 or ++.

<!DOCTYPE html>
<html>

<body>

  <button onclick="IncrementScore()"> Increment</button>

  <script>
    var score = 0;

    function IncrementScore() {
      score++;
      console.log(score);
    }

    console.log(score);
  </script>
</body>

</html>

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

Comments

0

Change your code to score += 1 and move you console call to inside of your function.

<!DOCTYPE html>
<html>

<head>
	<meta charset = "utf-8">
	<title>Increment Button</title>
</head>

<body>

<button onclick="IncrementScore()"> Increment</button>

<script>
	var score = 0;
	
	function IncrementScore()
	{
		
		score += 1;
    console.log(score);
	}
	
	console.log(score);
</script>
</body>


</html>

4 Comments

Why should he change score++ to score += 1? They're essentially the same.
Please do not post code to 3rd party sites, like Fiddles or CodePens as these links can die over time and make your answer meaningless. Post your code right here in your answer as a code snippet.(use the <> button while composing your answer).
@ScottMarcus I did not know that. Thanks for the info
@Barmar just showing OP that there are different ways to get the result he wants.
0

On each click you increment variable. And in next line assign to variable "1". You should delete line

score = 1;

Comments

0

TL;DR: Actually the score did increment, but not be printed out via the click handler.

As I know in your snippet, all your script in <script> tag (include console.log) will all execute once initially. Then it sits there to listen to the events, like your click. Then when you click on the button, the IncrementScore function is called, it increments the score variable, but not print it out. You know why? because you don't tell it to do so, (in the IncrementScore handler). If you notice, you'll see that you only have one 0 printed out, not each 0 per click.

You should read about the call stack,etc.. to know more about the order which code is executed... The fixes snippet can be found in other's answer, here is the code for when the variable actually "does not change", and be printed out for each click.

<!DOCTYPE html>
<html>
<body>

<button onclick="IncrementScore()"> Increment</button>

<script>
  var score = 0;
  function IncrementScore() 	{
    //score++;
    console.log(score);
  }
</script>
</body>
</html>

3 Comments

oh, I understand what you mean. However now in my actual code, i fixed it up but getting another problem. i see the results changing in the console but it flashes and disappears
@JosiahFrancis oh, where is your "actual code", did you show it?
no, i did not show it but i solved the problem i did something silly which was causing the problem.

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.