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

Konstantin Ruzhev
Konstantin Ruzhev
6,335 Points

My Solution

var questions = [

['How many Planets are there', 9], ['How many figers do i have', 5], ['My lucky number is', 9] ];

var rightAnswer = []; var wrongAnswer = []; var wrongAnswerShow = [];

var correct = 0; var question; var answer; var response; var html;

for (var i = 0; i < questions.length; i += 1){

question = questions[i][0]; // get the question answer = questions[i][1]; // get the answer response = parseInt(prompt(question)); // ask the question if (response === answer) { // check reposnse against the answer

correct += 1; // if answer is response is equal to asnwer add 1 to the correct variable
rightAnswer.push(question); //stores right answers 

}else { wrongAnswer.push(question); // stores wrong asnwers wrongAnswerShow.push(answer); // stores correct asnwer

} }

html = "You got " + correct + " question(s) right. ";

var isFalse = "<h1> Questions You got wrong were </h1> </br> <ol>"; // if asnwer is false var isTrue = "<h1> Questions You got right </h1> </br> <ol>" ; // if asnwer is true

if(rightAnswer.length !== 0) { for (var i = 0; i < rightAnswer.length; i += 1){

isTrue += '<li>' + rightAnswer[i] + '</li>';

}

} else {

isTrue += "<p> You did not get any Q right </p>";

}

if(wrongAnswer.length !== 0) { for (var i = 0; i < wrongAnswer.length; i += 1){

isFalse += '<li>' + "<p>" + wrongAnswer[i] + "</p>" + "<p> Correct Asnwer is " + wrongAnswerShow[i] + " </p>" +  '</li>';

}

}

isTrue += "</ol>"; isFalse += "</ol>"; var score = "<h1> You have " + rightAnswer.length+"/"+questions.length + " correct</h1>"; print(html + isTrue + isFalse + score);

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