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

Richard Preske
PLUS
Richard Preske
Courses Plus Student 4,356 Points

My ordered list won't print out. Any suggestions? I used the instructor's build list function.

Here's my code:

var quizQuestions = [ ['What is the largest Continent?', 'ASIA'], ['Name the branch of government where laws are interpreted.', 'SUPREME COURT'], ['What is 50*70?', '3500'], ['Name the largest Ocean.', 'PACIFIC OCEAN'] ]; var CorrectAnswers = []; var WrongAnswers = []; var guess; var guessCorrect = 0; var answer; var html; var guessWrong = 0;

function Buildlist(array) { var listhtml = '<ol>'; for (var i = 0; i < array[i]; i += 1) { listhtml = '<li>' + array[i] + '</li>'; } listhtml += '</ol>'; return listhtml; }

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

function questions () { for (var i = 0; i < quizQuestions.length; i += 1){ guess = prompt(quizQuestions[i][0]); answer = quizQuestions[i][1]; guess = guess.toUpperCase(); if ( guess === answer ) { guessCorrect += 1; console.log(CorrectAnswers.push(quizQuestions)); } else { console.log(WrongAnswers.push(quizQuestions)); } } } questions(); html = '<h2>You guessed '; html += guessCorrect + ' questions correct.</h2>'; html += '<h3>These are the questions you got right:</h3>'; html += Buildlist(CorrectAnswers); html += '<h4>These are the questions you got wrong: </h4>'; html += Buildlist(WrongAnswers); print(html);

6 Answers

Steven Parker
Steven Parker
231,275 Points

Richard Preske — Normally, you would mark "best answer" on the answer that contains the information that helped solve the issue! (You can still change it).   :see_no_evil:

Steven Parker
Steven Parker
231,275 Points

First, the code should add just one question at a time to the correct/wrong arrays:

      console.log(CorrectAnswers.push(quizQuestions[i][0]));  // one instead of all

Then, in "Buildlist":

  for (var i = 0; i < array.length; i += 1) {    // length instead of an element
    listhtml += "<li>" + array[i] + "</li>";     // "+=" instead of just "="

UPDATE: It looks like you overlooked one of my previous fix suggestions:

  for (var i = 0; i < array[i]; i += 1) {      // you still have this
  for (var i = 0; i < array.length; i += 1) {  // it should be length instead of an element
Richard Preske
PLUS
Richard Preske
Courses Plus Student 4,356 Points

Thank you. Caught the second part last night. The first part I didn't know, thanks. However, when I tried running again it's still not printing the list of wrong, correct answers.

Steven Parker
Steven Parker
231,275 Points

The correction shown in the first example needs to be applied to both the correct and wrong array pushes.

Richard Preske
PLUS
Richard Preske
Courses Plus Student 4,356 Points

yes I did still not showing the list. I have a screenshot of the output to console.log. How do I send a screen shot? Thanks.

Steven Parker
Steven Parker
231,275 Points

You can upload an image (or other file) into a workspace, but the workspace snapshot link itself should be enough to make the issue easy to replicate.

And the console log will only show the array lengths after each "push". The other output is displayed on the web page.

Richard Preske
PLUS
Richard Preske
Courses Plus Student 4,356 Points

I just copied it here:

Failed to load resource: net::ERR_FILE_NOT_FOUND quiz.js:39 1 quiz.js:37 1 quiz.js:37 2 quiz.js:39 2 quiz.js:17 Array(2)0: "Name the branch of government where laws are interpreted."1: "What is 50*70?"length: 2_proto: Array(0) quiz.js:22 <ol></ol> quiz.js:48 <h2>You guessed 2 questions correct.</h2><h3>These are the questions you got right:</h3><ol></ol> quiz.js:17 Array(2)0: "What is the largest Continent?"1: "Name the largest Ocean."length: 2proto_: Array(0) quiz.js:22 <ol></ol>

Steven Parker
Steven Parker
231,275 Points

Unformatted code pasting (where all the lines get compressed) isn't very helpful. But if you make a snapshot of your workspace and post the link to it here, the entire environment can be perfectly replicated for examination.