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

Bram Lo
Bram Lo
1,590 Points

My code isn't displaying the correct / wrong answers

http://w.trhou.se/n7dq4mzenu

My code doesn't give an error so I can't find the part that isn't working.

8 Answers

Hey,

you are most welcome.

Regards,

Richard.

Hey,

Can you please post your code in the thread please? I can't view your workspace. If you are having trouble formatting the code in the thread you can try viewing the markdown cheat sheet!

Regards,

Richard.

Bram Lo
Bram Lo
1,590 Points
/* 
  This is the quiz app.
  The program keeps track of the number of questions the user got correct.
  After all questions the progrma will display the number of questions that were answered correctly;
    It will show what questions the user got correct
    And it will show what questions were wrong.
  The questions and the answers are stored in an array.
  A loop is used for cycling through the array. It's also used to compare the response from the player to the answer in the array.
  The prompt will be used to ask the question.
*/

var correctAnswers = 0;
var wrongAnswers = 0;
var message_correctAnswers;
var correct = [];
var wrong = [];
var quizQuestions = [
  ['How many days are there in a week?', 7],
  ['What color is blood?', 'red'],
  ['How many starting HP does each hero start with in HearthStone?', 30]
];

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 < quizQuestions.length; i += 1) {
  var answer = prompt(quizQuestions[i][0]);
  if (parseInt(answer) === quizQuestions[i][1] || answer.toLowerCase() === quizQuestions[i][1]) {
    correctAnswers += 1;
    correct.push(quizQuestions[i][0]);
  } else {
    wrong.push(quizQuestions[i][0]);
  }
}

message_correctAnswers = 'You got ' + correctAnswers + ' answers correct.';
print(message_correctAnswers);
buildList(correct);
buildList(wrong);
Bram Lo
Bram Lo
1,590 Points

Here's the code, thanks for trying to help!

Hey,

outputDiv.innerHTML = message you forgot the semicolon.

Regards,

Richard.

Bram Lo
Bram Lo
1,590 Points

I added the semicolon still doesn't print the <ol>; and the first message is printing so that isn't the problem I think.

Hey,

Is the error in the HTML, I can't seem to see any errors here. Can you please post your HTML?

Regards,

Richard.

Bram Lo
Bram Lo
1,590 Points

That's the other problem, I don't see an error myself when I open the Dev Tools.

Hey,

I got it working:

<div id="output"></div>
<script>
/* 
  This is the quiz app.
  The program keeps track of the number of questions the user got correct.
  After all questions the progrma will display the number of questions that were answered correctly;
    It will show what questions the user got correct
    And it will show what questions were wrong.
  The questions and the answers are stored in an array.
  A loop is used for cycling through the array. It's also used to compare the response from the player to the answer in the array.
  The prompt will be used to ask the question.
*/

var correctAnswers = 0;
var wrongAnswers = 0;
var message_correctAnswers;
var correct = [];
var wrong = [];
var quizQuestions = [
  ['How many days are there in a week?', 7],
  ['What color is blood?', 'red'],
  ['How many starting HP does each hero start with in HearthStone?', 30]
];

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 < quizQuestions.length; i += 1) {
  var answer = prompt(quizQuestions[i][0]);
  if (parseInt(answer) === quizQuestions[i][1] || answer.toLowerCase() === quizQuestions[i][1]) {
    correctAnswers += 1;
    correct.push(quizQuestions[i][0]);
  } else {
    wrong.push(quizQuestions[i][0]);
  }
}

message_correctAnswers = 'You got ' + correctAnswers + ' answers correct.';
print(message_correctAnswers);
buildList(correct);
buildList(wrong);
</script>

Try running this in a HTML file. It worked for me in workspaces.

Regards,

Richard.

Bram Lo
Bram Lo
1,590 Points

Still isn't working >.< Even when I try to run it in the html file itself.

Hey,

What browser are you using?

Also this needs to go in a file called <filename>.html and then view it on workspaces, by clicking the eye in the top right hand corner of workspaces. You may have clicked prevent this page from creating additional dialogues.

Regards,

Richard.

Bram Lo
Bram Lo
1,590 Points

I know, I deleted the .js file and put it in the html filed it was linked to. I am using Chrome.

Hey,

did you view the page by selecting the file named <filename>.html and then clicking the eye icon?

Regards,

Richard.

Bram Lo
Bram Lo
1,590 Points

Hi, Yes I did

Hey,

Hm... I don't know why this isn't working. try this and paste the code I have written in there and see if it works. http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro .

Regards,

Richard.

Bram Lo
Bram Lo
1,590 Points

I just tried it. Same result: only seeing the amount of questions the user got right not the list. Thanks for your help man I think I'm just going to copy the cody of the course. I understand all of it and mine is almost exactly the same though it doesn't work somehow. Again thanks for the effort.

Regards,

Bram