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

Shreyas Sreenivasa
Shreyas Sreenivasa
7,804 Points

'Undefined' is replaced with the no of correct questions

I feel my code is proper but instead of displaying the no. of questions correct 'undefined' appears.

My code:

var questonsAndAnser = [['What is 1 + 1?', '2'], 
                        ['How many states are there in India?', '29'], 
                        ['How many planets are there in Solar System', '8']];
var question;
var correctAnswers;
var answer;
var response;
var html = "";

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


for (var i = 0; i < questonsAndAnser.length; i += 1) {
  question = questonsAndAnser[i][0];
  answer = questonsAndAnser[i][1];
  response = parseInt(prompt(question));

  if (response === answer) {
    correctAnswers += 1;
  } 
}

html = "You got " + correctAnswers + " question(s) right.";
print(html)

2 Answers

Jeff Wilton
Jeff Wilton
16,646 Points

I see two different issues with your code. First, your answers are string literals, '2', '29', and '8'. But when you are receiving the player's answers, you are forcing them to integers. And then when you are comparing the two, you are using the triple equals which will first check if the types match, which they never will (string != int). The fix is simple, just decide if you want to work with strings or integers and use the same on both sides.

Second issue, probably the one causing the undefined error is that you are trying to add an integer to 'correctAnswers' but that variable has not been initialized yet. You might as well just set it to 0 when you declare it:

var correctAnswers = 0;

Hope this helps!

Karolin Rafalski
Karolin Rafalski
11,368 Points

The first thing to do would be to add a console.log inside your if (response === answer){console.log('we are in this code block!')} - this will let you know if, in fact, your if statement is valid/working the way you expect.

When you run your quiz, you should see that even though you put the correct answer in, this console.log does not show up.

The reason is that you are putting the correct answer as a string (i.e. '2'), and then in the quiz portion, you converting the answer with parseInt into a number.

2 does not = '2' when you use ===

2 will equal '2' if you use ==

By default, any entry via prompt will return a string.

You can solve this in three ways, you can remove the parseInt or you can take the quotes off the numeric answers in your array or you can just use == instead of === (although I don't recommend this last method).

I hope this helps!