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

Andrew Rodrigues
Andrew Rodrigues
6,159 Points

What do you guys think of my code?

function main(questionList) {
    for (var i = 0; i < questionList.length; i += 1) {
        response = prompt(questionList[i][0]);
        if (response.toLowerCase() === questionList[i][1]) {
            rightAnswers.push(questionList[i][0]);
        } else {
            wrongAnswers.push(questionList[i][0]);
        }
    }
}

/* Takes an argument for the list that you want to print out
along with a string determining if it's right or wrong
which inserts it into the string instead of writing it twice*/
function printEach(list, rightWrong) {
    document.write('<hr>');
    document.write('<h1>You got the following ' 
        + rightWrong + '! (' + list.length + ')</h1>');
    for (var i = 0; i < list.length; i += 1) {
        document.write('<p>' + list[i] + '</p>');
    }
}

/*Takes the above function and uses it on both arrays
to pass arguments and print them out*/
function printOut() {
    printEach(rightAnswers, "right");
    printEach(wrongAnswers, "wrong");
}

//These are the questions and answers
var questionsAnswers = [
    ["What is my last name?", "rodrigues"],
    ['What is my favorite color?', 'black'],
    ['What is my college major?', 'computer science'],
];

//Setting up arrays
var rightAnswers = [];
var wrongAnswers = [];

/*Running the main function in order to ask the questions, 
then push whether the answer is correct or not to another array*/
main(questionsAnswers);
printOut();

What do you guys think? I would appreciate any comments or tips or anything to improve this. I started with a lot of code, then made a few functions to size it down and make it more flexible! Thanks everyone!

1 Answer

Great Job Andrew! Code looks great.