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

Aaron Lipinski
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Aaron Lipinski
Full Stack JavaScript Techdegree Graduate 15,990 Points

Stuck and need a little nudge.

So I'm currently working on the second part of the Quiz Challenge. I'm stuck figuring out how to Add a question to an array if its correctly answered by the user, or add it the array if it's wrong. Then print those questions with HTML stating which question were correct or incorrect. Here is my code so far:

var questions = [ ['Whats the capitol city of Scotland?', 'Glasgow'], ['What programing language is named after a snake?', 'Python'], ['Where would you find the Sea of Tranquility?', 'The Moon'] ]; var correct = []; var wrong = []; var correctAnswer = 0; var question; var answer; var response; var html; var listHtml;

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

function printCorrect( correct ){ listHtml = '<ol>'; if ( response === answer ){ listHtml += '<li>' + questions[i][0] + '</li>'; }

}

for ( var i = 0; i < questions.length; i += 1){ question = questions[i][0]; answer = questions[i][1]; response = prompt(question); if ( response === answer ){ correctAnswer += 1; } }

html = 'You got ' + correctAnswer + ' question(s) correct!'; print(html)

2 Answers

Steven Parker
Steven Parker
229,952 Points

Here's some hints:

Where you increase the "correctAnswer", you could also add the current "question" to the "correct" array. The "push" method could be handy for that. You could add an "else" block and do the same thing for the "wrong" array.

Then when you output the result, you could have loops for each of the arrays to add them to the "html" before finally printing it to the page.

And when posting code to the forum, use Markdown formatting to preserve the appearance, or make a snapshot of your workspace and post the link to it.

Aaron Lipinski
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Aaron Lipinski
Full Stack JavaScript Techdegree Graduate 15,990 Points

Hey Steven,

Thanks for the feedback. I finally figured out I was using the .push method incorrectly. I wrote it as

correct = correct.push(question)

instead of

correct.push(question).