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

Aaron Hasbrouck
Aaron Hasbrouck
1,295 Points

Couple questions on lists and undefined

Okay so a couple questions with building this test. How do I make the program only post one correct/incorrect answer per bullet point. The program is currently just posting a bullet point for every correct answer and displaying the array. I am also not sure why it is posting undefined before the amount correct and if a field is empty.

Thank You! https://w.trhou.se/s74tg3i1kk

Aaron Hasbrouck
Aaron Hasbrouck
1,295 Points

I was able to sort out the list thing, but I still have the problem with it saying undefined if there is nothing in the field

Andrew Cline
Andrew Cline
7,447 Points

Hey Aaron,

Now that you have fixed the list problem, can you post your updated code that you are working with?

1 Answer

Andrew Cline
Andrew Cline
7,447 Points

Here is my solution, I think that did what you were asking.

I also made some changes to your code, I tried to comment each change I made. I would put this code into another workspace and compare to your current one.

Let me know if you have any questions.

//Data

// first I changed the array of arrays into an array of objects. 
// I think it makes the code a little bit easier to read.
var questions = [
  {
    question: 'How many states are in the United States?',
    answer: 50
  }, 
  {
    question: 'How many continents are there?',
    answer: 7
  }, 
  {
    question: 'How many legs does an insect have?',
    answer: 6
  }
];

// variables

var correctQuestion = [];
var incorrectQuestion = [];

var correctAnswers = 0;
var html;

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

// I removed the function printList, I don't think you ever called it. 

for (var i = 0; i < questions.length; i += 1) {
  // removed variables question and answer here, I think this makes the code more dry
  response = prompt(questions[i].question);
  response = parseInt(response);
  if (response === questions[i].answer) {
    correctAnswers += 1;
    correctQuestion.push(questions[i])//pushes object to correctQuestions;
//    print('<h1>Congraulations, you\'ve answered these questions correctly</h1>');
  } else if (response != questions[i].answer){
     incorrectQuestion.push(questions[i]);//pushes object to incorrectQuestion
     // print('<h1>Sorry, you\'ve answered wrong on these questions</h1>');  
  }
}

html = "<h1>You got these question(s) right.</h1>"

//list of correct questions
html += "<ul>"

for (var i = 0; i < correctQuestion.length; i += 1) {
  html += "<li>" + correctQuestion[i].question + "</li>"; 
}

html += "</ul>"; 
// list of wrong questions and the answer
html += "<h1>You got these questions wrong</h1>"

html += "<ul>"

for (var i = 0; i < incorrectQuestion.length; i += 1) {
  html += "<li>" + incorrectQuestion[i].question + " ";
  html += "the correct answer is " + incorrectQuestion[i].answer + "</li>";
}

html += "</ul>"; 


print(html);