Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Hi! I’m stuck at step 9 even though the console log is showing that code is working fine: a question is being selected and it also gives an answer selected from the choices of the same object.

Your code so far

const questions = [
  {category:"Books",
    question: "Who wrote The Chronicles of Narnia?",
    choices: ["Lewis", "Tolkien", "Rowling"],
    answer: "Lewis",
}, {category:"Sports",
    question: "Who is considered to be the GOAT in football?",
    choices: ["CR7", "Ronaldinho", "Messi"],
    answer: "Messi",
}, {category:"Videogames",
    question: "What series is the spell Firaga from?",
    choices: ["FF", "Baldur's Gate", "The Witcher"],
    answer: "FF",
}, {category:"Characters",
    question: "What's the name of Phineas and Pherb's pet?",
    choices: ["Araku", "Pluto", "Perry"],
    answer: "Perry",
}, {category:"Films",
    question: "Best trilogy eva?",
    choices: ["LoTR", "Shrek", "Harry Potter"],
    answer: "LoTR",
}];


const getRandomQuestion = function(arr) {
   let random = Math.floor(Math.random() * arr.length);
   return arr[random]
};

let randomQuestion = getRandomQuestion(questions);
console.log(randomQuestion)

const getRandomComputerChoice = function(arr) {
  let random = Math.floor(Math.random() * 3);
  return arr['choices'][random];
};

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

const getResults = (question, choice) => {
  if (choice === question.answer) {
    return `The computer's choice is correct!`
  } else {
    return `The computer's choice is wrong. The correct answer is: ${question.answer}`
  }
}
let results = getResults(randomQuestion, computerChoice);
console.log(results) 


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

The app may work but it might not follow the rules of the user stories and tests.

Are you stuck on Step 9 or Test 9?

I meant test 9. Sorry.

You should have a function named getRandomComputerChoice that takes the array of the available choices as a parameter,

If you have given the array of available choices as an argument

return arr['choices'][random];

Then why do you need to specify the choices key here? An array would not have a key.

Well, I’m not actually passing the array of available choices but the question as an object. Then it proceeds to access the choices keys, which is an array. Not sure why is not working. The other tests are fine, this is the only one that throws an error.

Right, because you haven’t followed the user story 7

Sorry, I see your reply doesn’t show the same user story I’m seeing here on the exercise page. It says:

  1. You should have a function named getRandomQuestion that takes an array of questions as a parameter and returns a random question object from the array.

This is what I did to follow user story 7:

const getRandomQuestion = function(arr) {
   let random = Math.floor(Math.random() * arr.length);
   return arr[random]
};

Dont know what’s wrong with it. And when I run it, it marks this test as passed:

  1. You should have a function named getRandomQuestion that takes an array of questions as a parameter and returns a random question object from the array.

My bad, it’s user story 8 about the getRandomComputerChoice. Same problem.

  1. You should have a function named getRandomComputerChoice that takes the array of the available choices as a parameter, and returns a random answer to the selected question.

And this is why test 9 fails.

  1. You should have a function named getRandomComputerChoice that takes the array of the available choices as a parameter,

Got it to work.
Thanks a lot for the help!

1 Like