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 2 Solution

brendon hanson
brendon hanson
6,191 Points

Why is this Code not working?

The only error in the console says unexpected token at line 30, but it doesn't make sense because the token should be there...

// Keeps track of how many questions are answered right var correctGuess = 0;

// helps organize better var question;

// The RIGHT answer to the question var answer;

// The users guess var response;

// What is printed to the page var html;

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

//Multi-demi Array holding the questions and answers var questions = [ ["How many continents are there?", 7],

["What year is it?", 2017],

["How old is vlad?", 28] ];

var correct [ ];

var wrong [ ];

// Loop that asks questions and determines whether response it's right or wrong for (i = 0; i < questions.length; i += 1) { var question = questions[i][0]; var answer = questions[i][1]; var response = parseInt(prompt( question )); if (response === answer) { correct.push( question ); correctGuess += 1;

} else {

wrong.push( question );

} } // This Function Creates the list

function printlist( list ) {

var listHTML = '<ol>';

for(i = 0; i < list.length; i += 1) {

listHTML = '<li>' + list[i] + '</li>';

} listHTML += '<ol>'; return listHTML; }

// This shows how many you got right and puts the wrong and right in different spots

html = "You Got " + correctGuess + " questions(s) Right!";

html += '<h2>' You Got These Questions Right: '</h2>';

html += printList(correct);

html += '<h2>' You Got These Wrong: '</h2>';

html += printList(wrong);

print(html);

1 Answer

There are a few reasons this isn't working.

First, you are not declaring the array correctly here:

var correct [ ];

This is why you're getting the "unexpected token" error. You want to do it this way:

var correct =  [ ];

Same goes for the "wrong" variable.

Next, look at the difference in the way you are calling the printList function and how it is declared. Remember that javascript is a case-sensitive language.

Lastly, you are closing your h2 tags but attempting to write plain text in between them.

Also, you also want to append each answer to the listHtml variable in your loop. As is, you are assigning a new value each time you loop through the correct/incorrect answers.

brendon hanson
brendon hanson
6,191 Points

Thanks for the help! I got everything you said except for that last part talking about appending each answer to listHTML variable could you give me an example of this...the code works fine except that when I finish the quiz it only shows one right and one wrong which i'm sure has to do with what you were talking about at the end

When you write this in your loop:

listHTML = '<li>' + list[i] + '</li>';

You are assigning a new value to the listHTML variable, therefore overwriting the previous value. You want to append the list item to your variable, as you're doing with the html variable, like this:

listHTML += '<li>' + list[i] + '</li>';
brendon hanson
brendon hanson
6,191 Points

Thanks, sorry for taking up your time.

All good!