Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I don’t pass tests 8 and 9 but I dont get what i’m doing wrong.

and I pass test 13 but questionObj.answer gives undefined. How do I get the answer thas belongs to the question?

Your code so far

let questions=[
{
 category:"color? ", 
 question:"what is your favorite color?",
 choices:["blue","pink","green"],
 answer:"green",
},
{
 category:"living place?", 
 question:"Where do you live?",
 choices:["Bursa","Ankara","Istanbul"],
 answer:"Istanbul",
},
{
 category:"job?", 
 question:"What is your job?",
 choices:["computer-specialist","pro tennisplayer","teacher"],
 answer:"computer-specialist",
},
{
   category:"cooking?", 
 question:"What is your favorite food?",
 choices:["Pizza","Egg","Rice"],
 answer:"Pizza",
},
{
 category:"sports?", 
 question:"What is your favorite sport?",
 choices:["football", "volleyball","swimming"],
 answer:"football",
}

];

let currentQuestionIndex = null;

function getRandomQuestion(){
 currentQuestionIndex = Math.floor(Math.random() * questions.length);
  return questions[currentQuestionIndex].question;
}

function getRandomComputerChoice() {
let choices = questions[currentQuestionIndex].choices;
  let choiceIndex = Math.floor(Math.random() * choices.length);
  return choices[choiceIndex];
}

let questionObj = getRandomQuestion()
console.log(questionObj)

let computerChoice = getRandomComputerChoice() 
console.log(computerChoice)

function getResults(questionObj, computerChoice) {
  if (computerChoice === questionObj.answer) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${questionObj.answer}`;
  }
}
 
console.log(getResults(questionObj, computerChoice));




Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Here are some troubleshooting steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the first failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

  1. You should have a function named getRandomQuestion that takes an array of questions as a parameter

Your function has no parameters.

function getRandomQuestion(){
1 Like

Thanks for your reply, I created an array of questions with:

const questionArray = questions.map(q => q.question);

So now i passed test 8 but i was wondering if there is another way to create this array?

and for 9 I moved:

let choices = questions[currentQuestionIndex].choices;

outside of the function and passed that as well.

I now pass all the codes and get the right outcome but I used this for the last part

function getResults(questionObj, computerChoice) {

if (computerChoice === questionObj.answer) {

return “The computer’s choice is correct!”;

} else {

return `The computer’s choice is wrong. The correct answer is: ${questionObj.answer}`;

}

}

let result = getResults(questions[currentQuestionIndex], computerChoice);

console.log(result);

But the test says: you should have a function named getResults that takes the question object as the first parameter and the computer’s choice as the second parameter

but is this:

let result = getResults(questions[currentQuestionIndex], computerChoice);

using the question object?

1 Like

Yes, it is :+1:

questions is an array of objects. You’ve used [currentQuestionIndex] to select one of the objects in the array.

1 Like