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

Chris Harkin
Chris Harkin
1,104 Points

Build a Quiz Challenge, Part 1

Hi guys am wondering if anyone can help me out with my code for this challenge. So far I have created the prompt dialog boxes to ask the questions and store the users answer in to a variable. I have also created a variable to keep the score and a 2D array to store the questions and the correct answers. See my code bellow.

// varibile to store users score 
var total;

// Asks the questions and soters users answers
var questionOne =  prompt ("This is Question one");
var questionTwo = prompt ("This is Question two");
var questionThree =  prompt ("This is Question Three");

// Two Dimensional array soring questions and answers.
var questionAnswers = [
  ["What color is the sky", "Blue"],
  ["What color is Grass", "Green"],
  ["Waht color is the Sun", "Yellow"] 
];

But I am looking for a way to pass the values from the question part from the questionAnswers array and passing them to the prompt dialog box. So the dialog box takes the questions from the array and prints them to the screen for the user to answer. Here is what I tried but it doesn't work.

var questionOne =  prompt (questionAnswers [0][0] );
var questionTwo =  prompt  (questionAnswers [1][0] );
var questionThree =  prompt  (questionAnswers [2][0] );

Can anyone tell me how to do this or if it’s even possible

2 Answers

Steven Parker
Steven Parker
231,275 Points

:point_right: You just need to move the question section after the definition of the question/answer array.

Once you do that, it works exactly as you intended.

Chris Harkin
Chris Harkin
1,104 Points

Hay Steven thanks for your help after I moved the questions to after the array it worked like I wanted it to. But the I wrote my function to check the users answers and the prompt box asking the questions stopped working Can you please help.

// varibile to store users score 
var total;

// Two Dimensional array soring questions and answers.
var questionAnswers = [
  ["What color is the sky", "Blue"],
  ["What color is Grass", "Green"],
  ["Waht color is the Sun", "Yellow"] 
];

var questionOne =  prompt (questionAnswers [0][0] );
var questionTwo =  prompt  (questionAnswers [1][0] );
var questionThree =  prompt  (questionAnswers [2][0]);

function Resasults () {
  if ( questionOne === questionAnswers [0][1] && questionTwo === questionAnswers [1][1] && questionThree === questionAnswers [2][1] ){
    total += 3;
    document.write (β€œCongratulations you answered  ” + total + β€œ questions correctly” );
  } else if ( questionOne === questionAnswers [0][1] && questionTwo === questionAnswers [1][1] && questionThree !== questionAnswers [2][1] ){
    total += 2;
    document.write (β€œWell done  ” + total + β€œ questions correctly” );
  } else if ( questionOne === questionAnswers [0][1] && questionTwo !== questionAnswers [1][1] && questionThree === questionAnswers [2][1] ){
    total += 2;
    document.write (β€œWell done  ” + total + β€œ questions correctly” );
  } else if ( questionOne !== questionAnswers [0][1] && questionTwo === questionAnswers [1][1] && questionThree === questionAnswers [2][1] ){
    total += 2;
    document.write (β€œWell done you answered  ” + total + β€œ questions correctly” );   
  } else if ( questionOne === questionAnswers [0][1] && questionTwo !== questionAnswers [1][1] && questionThree !== questionAnswers [2][1] ){
    total += 1;
    document.write (β€œSorry you only answered  ” + total + β€œ questions correctly” );

  } else if ( questionOne !== questionAnswers [0][1] && questionTwo !== questionAnswers [1][1] && questionThree === questionAnswers [2][1] ){
    total += 1;
    document.write (β€œSorry you only answered ” + total + β€œ questions correctly” );
  }else if ( questionOne !== questionAnswers [0][1] && questionTwo === questionAnswers [1][1] && questionThree !== questionAnswers [2][1] ){
    total += 1;
    document.write (β€œSorry you only answered  ” + total + β€œ questions correctly” );
  } else {
    total = 0;
    document.write (β€œSorry you only answered  ” + total + β€œ questions correctly” );
  }
}

Resasults();
Myriam De la pena
Myriam De la pena
2,942 Points

well, I would start by making a for loop that cycles throw the questions instead of doing else if so many times and every every code add a counter to the total.

Chris Harkin
Chris Harkin
1,104 Points

Myriam De la pena Thanks so I took your advice and tried to put it in to a for loop. Am not sure if my solution will work as the prompt box still aren't appearing any ides why and if my code will even work?

// Two Dimensional array soring questions and answers.
var questionAnswers = [
  ["What color is the sky", "Blue"],
  ["What color is Grass", "Green"],
  ["Waht color is the Sun", "Yellow"] 
];

// variable users answer, correct answer and wrong answers 
var userAnswer = [];
var correctAnswer = [];
var wrongAnswer = [];

// check the users answers
for (i = 0; i , questionAnswers.length i += 1) {
  userAnswer = prompt (questionAnswers [i][0]);
  if (answer === questionAnswers [i][1]){
    correctAnswer.push (questionAnswers [i][0]);
  }else{
    wrongAnswer.push (questionAnswers [i][0]);
  }
}

// tell the user how many questions the got correct and  which questions where correct and which question they got wrong.
document.write ("<h1>Congratulations you got " + correctAnswer.length + " correct</h1>");
document.write ("<h2>Congratulations you answerd these questions correctly: </h2>" + correctAnswer.join ('<br>'));
document.write ("<h2>Sorry but you got these questions wrong: </h2>" + wrongAnswer.join ('<br>'));


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