Build a Quiz Game

I’m stuck my code passes all other steps except step 9…the code performs as intended though

const question1 = {
   category: "Science",
   question: "What is the chemical formula for water?",
   choices: ["H20", "HO2", "2HO"],
    answer: "H20",
 };
const question2 = {
   category: "Information Technology",
   question: "What is information?",
   choices: ["processed data", "raw input", "electronic device"],
   answer: "processed data"
 };
const question3 = {
   category: "Mathematics",
   question: "What is the derivative of a constant?",
   choices: ["1", "0", "constant"],
   answer: "0"
 };
const question4 = {
   category: "Physics",
   question: "What is the SI unit of power?",
   choices: ["watt(w)", "joules(j)", "amps(a)"],
   answer: "watt(w)"
 };
 const question5 = {
   category: "Geography",
   question: "What is the highest mountain on earth?",
   choices: ["Kilimanjaro", "Everest", "Mont Blanc"],
   answer: "Everest"
 };
const questions = [question1, question2, question3, question4, question5];

function getRandomQuestion(questionsArray){
  return questionsArray[Math.floor(Math.random()*questionsArray.length)] //Returns a question object
}
const currentQuestion = getRandomQuestion(questions);
console.log(currentQuestion);

function getRandomComputerChoice(questionObject){
  return  questionObject.choices[Math.floor(Math.random()*questionObject.choices.length)] //returns computer choice from the question object
}
const choice = getRandomComputerChoice(currentQuestion);
console.log(choice);

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

Please do not open duplicate topics.

Please share a link to the lab you are working on

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Got it, I had no idea I could do that.

1 Like
  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.