Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

sorin vasiliu
sorin vasiliu
6,228 Points

I can't increase the value with =+, it give me the value NaN. Help!

This is my code:

var score = 0;

// USER QUESTIONS!

var choice1 = prompt('DOTA2: What heroe\'s ultimate is called PULSE NOVA?');
if ( choice1.toUpperCase() === 'LESHRAC' ) {
  score += 1 && alert('Congratulation! You got one right!');
  document.write('<p>Question 1: Correct! </p>');
} else {
  alert('Auch! That\'s incorrect, but I\'m sure you know the next one!');
  document.write('<p>Question 1: False! </p>');
}

What I'm hoping is to increase the value of 'score' with +1 for each correct answer. At the end I will try to give a medal based on the value of 'score'. But what I wrote so far doesn't work.... it tells me score is a NaN when i doc.write it... How can I make this work ?

2 Answers

Hi Sorin! I believe the problem lies in this line.

score += 1 && alert('Congratulation! You got one right!');

The '&&' is what is called a logical operator and is used to compare true/false statements. If you eliminate that, and split that line into two like this, it should work alright!

score += 1;
alert('Congratulations! You got one right!');

Hopefully that answers your question, have a great day!

sorin vasiliu
sorin vasiliu
6,228 Points

Yup, that was it! Thanks for the explanation!

No problem! Have a great day and happy coding! :)