Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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! :)