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 Solution

Ahmed Hassan
Ahmed Hassan
5,533 Points

My solution to the challenge!

//Setting the questions/answers arrays:
ques_ans=[["Who is teaching JS on Treehouse?", "dave mcfarland"],
          ["What tag is responsible for inserting JS into HTML file?", "<script></script>"],
          ["What's the name of Array of Arrays?", "two-dimentional array"]
          ]


//Setting a document print function and questions tracker:
function print_text(message) {
    document.write(message);
}

var counter_correct=0;
var counter_wrong=0;


//Starting the core program:

print_text("<h2>Please answer the pop up questions</h2>");
for (i=0; i<ques_ans.length; i++) {
    answer=prompt(ques_ans[i][0]);
    if (answer.toLowerCase()===ques_ans[i][1]) {
        counter_correct++;
        print_text("<ul><li>Good Job! that's correct<ul><li>That's "+counter_correct+" correct answer(s) out of 3 questions</li></ul></li></ul>");
    } else {
        counter_wrong++
        print_text("<ul><li>Oops! sorry, you got that wrong :(<ul><li>That's "+counter_wrong+" wrong answer(s) out of 3 questions</li></ul></li></ul>");
    }
}

print_text("<p>Your Results are: "+counter_correct+" correct answer(s), and "+counter_wrong+" wrong answer(s).");

if (counter_correct>counter_wrong) {
    print_text("<h3>You have successfully passed the test</h3>");
} else {
    print_text("<h3>You have failed the test. Click <a href=\"test_page_goes_here.html\">here</a> to undergo the test again</h3>");
}