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

Prahlad Genung
Prahlad Genung
5,588 Points

Page asking questions, then not loading page content after. Keep getting the "something went wrong" message.

code block: '''javaScript var correctAnswers = 0; var questions = [ ['How many states are there in the contiguous United States?' , 48], ['How many continents are there?', 7], ['How many legs does a beholder have?', 0] ]; var question; var answer; var response; var html; var correctAnswers =0; 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 = parseInt(prompt(question)); if (response === answer) { correctAnswers +=1; correct.push(question); } else { wrong.push(question); } }

html = "<b>You answered " + correctAnswers + " questions right:</b>" html += '<b>You answered these questions correct:</b>' html += buildList(correct); html +='<b>You answered these questions incorrectly:</b>' html += buildList(wrong); print(html);

''' I'm sorry, but the comment box is not respecting line breaks.

2 Answers

weo3
seal-mask
.a{fill-rule:evenodd;}techdegree
weo3
Full Stack JavaScript Techdegree Student 11,256 Points
var correctAnswers = 0;
var questions = [["How many states are there in the contiguous United States?" , 48],["How many continents are there?", 7],["How many legs does a beholder have?", 0]];
var question;
var answer;
var response;
var html;
var correct = [];
var wrong = [];

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

function buildList(arr) {
  "use strict";
  var listHtml = "<ol>";
  for (var i=0; i < arr.length; i++) {
    listHtml += "<li>" + arr[i] + "</li>";
  }
  listHtml += "</ol>";
  return listHtml;
}


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

Prahlad, I have cleaned up your code, and using the Markdown Cheatsheet link, was able to post it here so you can view it.

I am unable to test it at this point in time; however - some things to note, for your future reference:

  • you had correctAnswers declared twice
  • your for loop syntax was incorrect; instead of "i+=1", it should be "i++".
  • use double quotes, not single, when handling strings
  • your opening tag for the unordered list was incorrect. It was "ol", but should be "<ol>"
  • since you have two for loops within the same scope, I took the liberty of using "j" instead of "i" for your second one
  • I put in the "use strict" pragma inside your function calls and hopefully that will generate a more verbose response in the error handling process that can help you assess what to fix next

Keep at it, you will get it!

Prahlad Genung
Prahlad Genung
5,588 Points

Thank you. I really appreciate it.

weo3
seal-mask
.a{fill-rule:evenodd;}techdegree
weo3
Full Stack JavaScript Techdegree Student 11,256 Points

No problem! Let me know if you get stuck again. I will be able to get back to you tomorrow morning before 10amPST (GMT -8).