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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Thomas Chappel
Thomas Chappel
6,342 Points

Why I receive error NaN?

Why, if I declare the variable countCorrectAnswers undefined it returns me NaN et the end when I print the total score? While if I declare it as countCorrectAnswers = 0; everything works just fine?

html

var countCorrectAnswers ;
var correctAnswers= [];
var wrongAnswers = [];


function print(message) {
  document.write(message);
}

function createList( list ) {
  var listHTML = '<ol>';
  for ( var i = 0; i < list.length; i += 1) {
    listHTML += '<li>' + list[i] + '</li>';
  }
  listHTML += '</ol>';
  return listHTML;
}


// two-dimensional array that holds quizzes and answers
var quizzes = [
  ['What is called the red planet?', 'mars'], 
  ['what is italian favorite sport?', 'soccer'], 
  ['What is the capital of Italy?', 'rome']
];



for (let i = 0; i < quizzes.length; i++) { 
  var answer = prompt(quizzes[i][0]);
  var answer = answer.toLowerCase();
  if (answer === quizzes[i][1]) {
    alert('correct answer!')
    countCorrectAnswers ++;
    correctAnswers.push(quizzes[i]);
  } else {
    alert('wrong answer!')
    wrongAnswers.push(quizzes[i]);
  }

}

print('<p> You guessed ' + countCorrectAnswers + ' answers right</p>')
print('<p> You got these quizzes right:</p>');
print(createList(correctAnswers));
print('<p> You got these quizzes wrong:</p>');
print(createList(wrongAnswers));

4 Answers

Michael Pashkov
Michael Pashkov
22,024 Points

In the beginning you should declare countCorrectAnswers; var countCorrectAnswers = 0; Then you var countCorrectAnswers+=1. But you have just var countCorrectAnswers;. And var countCorrectAnswers = nothing. And you begin counting nothing + nothing = error. html - remove it. What does it mean? once you declare "var answer" - you should not declare it more. Just answer = ... " var answer = prompt(quizzes[i][0]); var answer = answer.toLowerCase();"

Post your new changes

Michael Pashkov
Michael Pashkov
22,024 Points

Hi, look at var countCorrectAnswers. You declare it. But it has no number or string. html - look at this.

Thomas Chappel
Thomas Chappel
6,342 Points

But I want to know why

var countCorrectAnswers; and After, inside the loop, countCorrectAnswers ++ doesn't work; while var countCorrectAnswers=0; and After, inside the loop, countCorrectAnswers ++ works;

cannot we do "undefined" + 1?

Michael Pashkov
Michael Pashkov
22,024 Points

try countCorrectAnswers =0; countCorrectAnswers +=1;

Thomas Chappel
Thomas Chappel
6,342 Points

I know. I tried both options. I told from beginning that countCorrectAnswers +=1; works fine. I wanted to understand why it worked while var countCorrectAnswers; (undefined) does not work.

countCorrectAnswers; is a variable not initialized (no initial value). The reason why countCorrectAnswers is undefined is that you are adding a plus 1 to an undefined variable when you countCorrectAnswers++. The reason why countCorrectAnswers+= 1 works is you are defining the variable at that time. Best practice is to define a counter variable at the beginning and if need be reset that variable to 0 later on.