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

No idea what is wrong here

I tried to follow along with the solution of the quiz challenge video. I even stopped it several times to compare my code to the video. From what I can tell it is EXACTLY the same (except i shortened the questions at the top), but for whatever reason, it does not function properly (like in the video). I am going insane looking over the same code over and over not seeing the problem. Maybe someone else will see what I am missing ...

NOTE: found a few mistakes, now if i get all questions wrong it works properly, but if I answer a question correctly it stops the prompts and does not write anything to the document.... ??? so confused now

var questions = [
  ['How many US states?', 50],
  ['How many continents?', 7],
  ['Insect has how many legs?', 6]
];
var correctAnswers = 0;
var question;
var answer;
var response;
var corect = [];
var wrong = [];

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

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

for (var i = 0; i < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = prompt(question);
  response = parseInt(response);
  if (response === answer) {
    correctAnswers += 1;
    correct.push(question);
  } else {
    wrong.push(question);
  }
}

html = "You got " + correctAnswers + " question(s) right."
html += '<h2>You got these questions correct:</h2>';
html += buildList(corect);
html += '<h2>You got these questions wrong:</h2>';
html += buildList(wrong);
print(html);
print(html);

3 Answers

Found it. TYPO. Isn't it always. If you look at a few places the word "correct" was typed "corect".

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi there Jamison,

I saw that too but I didnt flag it because I saw your variable was also put down as corect, so I didn't mention it.

Are you all sorted now? :-)

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Jamison. Very good attempt on your code.

One or 2 of the lines in the html variable don't have a semi colon at the end of the statement, that would help.

But to be honest I don't see much wrong with it either, maybe someone has a keener eye than me. :)

I don't see where you have globally declared the var html so that will prevent the listing of the correct and incorrect answers.

You also could make your code a little cleaner by combining response = prompt(question); response = parseInt(response); into response = parseInt(prompt(question);

It's one less thing for the program to execute. Not such a big deal in such a tiny bit of code, but a good housekeeping habit that will pay off in large programs.