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 1

Jazz Atkin
Jazz Atkin
2,869 Points

Here's my attempt!

Threw in a tiny bit of extra code so that "You got these questions right" won't display if the user didn't get ANY right. No need to rub salt in the wound, hey. Would love feedback!

Also - how do I stop my code being greyed out below?

var correctCount = 0; // number of questions the user has answered correctly
var response; // user response;
var correctAnswers = []; // the questions that have been answered correctly 
var wrongAnswers = []; // the questions the have been answered incorrectly
var questions = [
  ['Which coast is New York on?', 'East'],
  ['Which coast is LA on?', 'West'],
  ['Which coast is Seattle on?', 'West']
];

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

for ( var i = 0; i < questions.length; i += 1 ) {
  response = prompt(questions[i][0]);
  answer = questions[i][1];
    if ( response.toLowerCase() === answer.toLowerCase() ) {
      correctCount += 1;
      correctAnswers.push(questions[i][0]);
    } else {
      wrongAnswers.push(questions[i][0]);
    }
}

html = 'You got ' + correctCount + '/3 questions right.';

if (correctCount > 0) {
   html += '<h2>You got these questions correct:</h2><ol><li>' + correctAnswers.join("</li><li>") + '</li></ol>'; 
} 
else {
     html += '<h2>You didnt get any questions correct.</h2>';
}

if (correctCount < questions.length) {
    html += '<h2>You got these questions wrong:</h2><ol><li>' + wrongAnswers.join("</li><li>") + '</li></ol>'; 
}
else {
    html +='<h2>You didnt get any questions wrong.</h2>';
}

print(html);

1 Answer

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Hi, Jazz! Very cool addition there! :) This is exactly the kind of thing that you would account for if you were writing some code that would be used by end users to polish the UX.

As for why your code gets greyed out, if you specify your code after the opening set of three backticks (css, javascript, etc), it will try and properly format it with colours. The only finicky one is php, for which you need to have an opening <?php tag before it will properly colour your code.

Keep up the great work! :)