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

Lukas Muller
Lukas Muller
7,347 Points

What do you think about my solution?

Hi everyone! I took on the challenge and this is my solution to it.

var questions = [["Who's the creator of Facebook?","Mark Zuckerberg"],
               ["Who's Apple's current CEO?","Tim Cook"],
               ["What's the name of the person who gets on my nerves the most?",                "Julia"]]
var rightQuestions = [];
var wrongQuestions = [];

var gotRight = 0;
for (i = 0; i < questions.length; i += 1) {
var answer = prompt( questions[i][0] );
if (answer === questions[i][1] ) {
 rightQuestions.push( questions[i][0] );
  alert("You are right!");
  gotRight += 1; 
 } else {
  wrongQuestions.push( questions[i][0] );
  alert("No! The answer would have been: " + questions[i][1]);
 }
}


document.write( "You got " + gotRight + " out of " + questions.length + " questions right.:" );
document.write("<h2>You got these questions correct:</h2>");
document.write(rightQuestions.join(", "));
document.write("<h2>You got these questions wrong:</h2>");
document.write(wrongQuestions.join(", "));

What do you guys think? I also improved it a bit, because my parents said it would be nice to see if you were right or wrong and if you were wrong, what the solution would have been.

1 Answer

Sam Baines
Sam Baines
4,315 Points

HI Lukas - I think what you have written so far is pretty good, especially as these challenges can be quite difficult to get to grips with for some, I have one main suggestion to make. Instead of repeating the document.write() function I would build a variable and then print that variable to the page instead. Also you code fails people if they don't capitalize the answer (which not everyone will do) - so I would suggest making sure the answers are either in all lower case and then use the .toLowerCase() method on the prompt to make sure the answers are not wrong due to this.

Like below:

var questions = [["Who's the creator of Facebook?","mark zuckerberg"],
               ["Who's Apple's current CEO?","tim cook"],
               ["What's the name of the person who gets on my nerves the most?",                "julia"]]
var rightQuestions = [];
var wrongQuestions = [];

var gotRight = 0;
for (i = 0; i < questions.length; i += 1) {
var answer = (prompt(questions[i][0]).toLowerCase());
if (answer === questions[i][1] ) {
 rightQuestions.push( questions[i][0] );
  alert("You are right!");
  gotRight += 1; 
 } else {
  wrongQuestions.push( questions[i][0] );
  alert("No! The answer would have been: " + questions[i][1]);
 }
}

var printMessage = ("You got " + gotRight + " out of " + questions.length + " questions right.:");
printMessage += ("<h2>You got these questions correct:</h2><br>" + rightQuestions.join(", "));
printMessage += ("<h2>You got these questions wrong:</h2><br>" + wrongQuestions.join(", "));

document.write(printMessage);

Hope this helps.