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

Some feedback for my solution?

// TeamTreeHouse 'Build a Quiz challenge 1' - quiz.js
var questions = [ //Some questions
    ['President of the US?', 'Obama'],
    ['President of the European Council?', 'Tusk'],
    ['President of France?', 'Hollande']
];
// Vars to compare the answers
var theRightAnswer, answerByUser;
// Which answers were right which wrong?
var rightAnswerIndex = [];
var wrongAnswerIndex = [];

for (i = 0; i < questions.length; i++) {
    answerByUser = prompt(questions[i][0]);
    theRightAnswer = questions[i][1];
    if(answerByUser === theRightAnswer) {
        console.log('You are right it is ' + theRightAnswer + ' !\n');
        rightAnswerIndex.push(i);
    } else {
        console.log('You are wrong it is' + theRightAnswer + ' !\n');
        wrongAnswerIndex.push(i);
    }
}
document.write('<p style="font-weight: bold">You answered ' + rightAnswerIndex.length + ' right!</p>');
if (rightAnswerIndex.length > 0) {
    document.write('<p>These are your right questions:</p>');
    for (i=0; i < rightAnswerIndex.length; i++){
    document.write('<p>' + questions[rightAnswerIndex[i]][[0]] + '</p>');}
} else {document.write('<p>Please try again!</p>');}

document.write('<p style="font-weight: bold">You answered ' + wrongAnswerIndex.length + ' wrong!</p>');
if (wrongAnswerIndex.length > 0) {
    document.write('<p>These are your wrong questions:</p>');
    for (i=0; i < wrongAnswerIndex.length; i++) {
    document.write('<p>' + questions[wrongAnswerIndex[i]][[0]] + '</p>');}
} else {document.write('<p>Yay no wrong answers!</p>');}

Hi Tobias,

Code looks good to me!

Where you've written:

var theRightAnswer, answerByUser;

Is this just creating two empty variables at once? Equivalent to:

var theRightAnswer;
var answerByUser;

I didn't know you could do that. Nice.

All the best,

Ede

Ede Harrison yep it does (or atleast it should, I dont know if it works in all browsers). I just did it because I am already used to do it this way in other languages. It just doesn't look good if you "waste" 5 lines to declare vars, bools, strings etc.