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

Mar Bocatcat
Mar Bocatcat
7,405 Points

My Solution to the Quiz Challenge (Feedback Please)

This is my solution for the Quiz Challenge, did not really have time to make it more fancy than it is. But thought its enough for what the challenge is looking for. Thanks!

    function print(message) {
    document.write('<p>' + message + '</p>');
    }

    var AnswerQuestions= [

    ['How many states are in the United States?', '50'],
    ['How many legs does an insect have?', '6'],
    ['What is the  capital of Illinois?', 'Chicago'],
    ];

    var rightQuestions = [];
    var wrongQuestions = [];



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

    var answer= prompt( AnswerQuestions[i][0] );

    if ( answer.toLowerCase() === AnswerQuestions[i][1].toLowerCase() ) {

    rightQuestions.unshift(AnswerQuestions[i][0]);
     } else {
    wrongQuestions.unshift(AnswerQuestions[i][0]);
      }
    }


    print('Right Questions answered:  ' + rightQuestions.join(', '));
    print('Wrong Questions answered:  ' + wrongQuestions.join(', '));
    print(' You answered ' + rightQuestions.length + ' questions right');

Where's your solution?

Mar Bocatcat
Mar Bocatcat
7,405 Points

Sorry its a little hard to read im not sure how to use Markup language for Treehouse. :p

//Fixed Code Presentation How to post code

1 Answer

rydavim
rydavim
18,814 Points

If you wanted to improve the formatting of the output, you could look into adding some additional HTML and CSS when the results are printed to the page. Certainly not a priority if you're focusing on JavaScript.

The JavaScript looks good to me. You solved it in a different way than I see most often, which is cool. The use of unshift is interesting, and not something I thought of when I did this challenge. Nice work!

My only suggestion going forward would be that tabbing-in your code blocks generally makes them a bit easier to read. This is to some extent personal preference, but it's a pretty common coding convention.

function print(message) {
  // Inside a code block, it makes it easier to read if you tab in.
  document.write('<p>' + message + '</p>'); 
}

If you're already doing this and the formatting just got lost when you posted it to the forums, obviously disregard.

Overall it looks good, happy coding! :)

Mar Bocatcat
Mar Bocatcat
7,405 Points

Thanks for your advice! Really appreciate, its the first language i tried that i actually have fun with the syntax.