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

sorin vasiliu
sorin vasiliu
6,228 Points

Build a Quiz Challenge, help with solution.

Hey, guys! This is my solution so far:

function print(message) {
  document.write(message);
}

var questionsAsked = [             //creating the multidimensional arrays with questions and answers
  ['Movies: Actor in Pulp Fiction - John....', 'travolta'],
  ['Series: The best game is the Game of....', 'thrones'],
  ['Series: Known amongst friends for when he says - How you doing?...', 'joey']
];

var correctAnswers = [];       // creating the necessary variables
var goodQuestions = [];
var badQuestions = [];

for ( var i = 0; i < questionsAsked.length ; i++ ) {    // using the for loop to prompt user input and compare answ
  var userAnswer = prompt( questionsAsked[i][0] );
    if ( userAnswer.toLowerCase() === questionsAsked[i][1] ) {
      correctAnswers.push(userAnswer);
      goodQuestions.push( questionsAsked[i][0] );
      alert('Booyah!');
    } else {
      alert('Auch!');
      badQuestions.push( questionsAsked[i][0] );      
    }
}

function printResults( questions ) {        // function for printing results in ol to screen
  var listHTML = '<ol>';
  for ( var i = 0; i < questions.length; i ++ ) {
    listHTML += '<li>' + questions[i][0] + '</li>';
  }
  listHTML += '</ol>';
  print(listHTML);
}

print('<h1>You got ' + correctAnswers.length + ' question(s) right.</h1>');

document.write('<h2>These are the questions you got right: </h2>');
printResults(goodQuestions);

document.write('<h2>These are the questions you got wrong: </h2>');
printResults(badQuestions);

The problem is at the end, when the program should list to the page the correct questions and the wrong answered questions in a list.

It just makes a list with the first letter from the arrays ( var goodQuestions, var badQuestions).

If I use the goodQuestions.toString(); the results show me that it stores the complete string, however with the printResults(function) it will only display the first letter of the string contained in the array.

Dunno why it does this exactly. I haven't looked at the solution just yet, hoping I'm not too far off.

Thanks!

This has nothing to do with code:

I like the cleverness of the alerts, but "Auch" is German for "also." Did you mean "Ouch"?

sorin vasiliu
sorin vasiliu
6,228 Points

Yep, that's what I wanted to say (Ouch!) :)) Now that you told me, I realized that "Auch" is german for also. I'm Romanian, and her we say ("Auci!") for ("Ouch!"), they are pronounced the same and have the same meaning... So, I guess I made a combination there with Auch :))

1 Answer

Hello Sorin,

the problem is with your output. goodQuestions and badQuestions are one dimensional arrays. Se here:

listHTML += '<li>' + questions[i][0] + '</li>';

The value of the first element for example is "Movies: Actor in Pulp Fiction - John...." and by adding [0] you are actually refering to the first character of the string which is M.

Here is how it should be:

listHTML += '<li>' + questions[i] + '</li>';

If you don't understand feel free to ask for more details.

sorin vasiliu
sorin vasiliu
6,228 Points

Indeed, that completely slipped my mind. Thank you for your help!