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

build a quiz challenge part 2... won't ask/alert any questions when page loads in chrome... copied Dave's code..nothing

var questions = [
  ['How many states are in the United States?', 50],
  ['How many continents are there?', 7],
  ['How many legs does an insect have?', 6]
];
var correctAnswers = 0;
var question;
var answer;
var response;
var correct = [];
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(correct);
html += '<h2>'You got these questions wrong:'</h2>';
html += buildList(wrong);

print(html);

Moderator edited: Question edited to add markdown to the code so that it renders properly on the forums. Please see the Markdown Cheatsheet at the bottom of the "Add an Answer" section to view instructions on how to post code.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there again, Victor! I've looked at your code and you have several errors which are preventing anything from happening at all. This is a great time to learn how to use your developer tools in the browser to track down errors! I highly suggest that you take a look at the errors and backtrace them like I did to find the cause of the problems.

That being said here's a problematic portion:

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

This section has one too many closed curly braces and one of the single quotes is misplaced. Both will cause errors and cease execution. It should look like this:

function buildList(arr){
    var listHTML = '<ol>';
    for(var i = 0; i < arr.length; i += 1){
        listHTML += '<li>' + arr[i] + '</li>';  //The ending single quote here should be between the closed angle bracket and semicolon
    }
    listHTML += '</ol>';
    return listHTML;
}
// Note the removal of the extraneous closed curly brace here

Further down in your code, you have some single quotes which are ending a string prematurely causing the interpreter to believe that the next part is a variable name (or several).

Your code:

html += '<h2>'You got these questions correct:'</h2>';  // You are ending the string around the h2 tags so it thinks the next part is a variable
html += buildList(correct);
html += '<h2>'You got these questions wrong:'</h2>';

It should look like this:

html += '<h2>You got these questions correct:</h2>';  //Note the removal of the inner pair of single quotes after the end of the beginning h2 tag and the beginning of the ending h2 tag
html += buildList(correct);
html += '<h2>You got these questions wrong:</h2>';

When I fix these, your code runs. I hope this helps! :sparkles:

Thank you so much.... seems I can grasp and understand the concepts quickly, but most of my problems arise from syntax errors or incorrect spelling... simple, avoidable errors... I read your response on my phone and ran the code again on the console and found the errors. Seems that I get frustrated and never seem to use the console for 'bugs'... I will definitely start using it now... Thank you for your time and consideration.