Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

My code is working fine but does not pass the test

Your code so far

const questions = [{
  category: "Food",
  question: "What is my favorite food?",
  choices: ["Pizza", "Fried Chicken", "Sushi"],
  answer: "Pizza"
}, {
  category: "Drink",
  question: "What is my favorite drink?",
  choices: ["Water", "Coke", "Alcohol"],
  answer: "Water"
}, {
  category: "Phone",
  question: "What is my phone brand?",
  choices: ["Huawei", "Apple", "Samsung"],
  answer: "Samsung"
}, {
  category: "Laptop",
  question: "What is my laptop brand?",
  choices: ["Acer", "Asus", "Mac"],
  answer: "Asus"
}, {
  category: "Math",
  question: "What is the square root of 81?",
  choices: ["3", "7", "9"],
  answer: "9"
}];

let randObj = Math.floor((Math.random() * questions.length))

function getRandomQuestion(randQuestion){
  return randQuestion[randObj].question;
}

console.log(getRandomQuestion(questions));

function getRandomComputerChoice(randChoice){
  return randChoice[randObj].choices[Math.floor((Math.random() * 3))]; 
}

let compChoice = getRandomComputerChoice(questions);

console.log(compChoice)

function getResults(){
  if (compChoice == questions[randObj].answer){
    return "The computer's choice is correct!"
  }
  else {
    return `The computer's choice is wrong. The correct answer is: ${questions[randObj].answer}`
  }
}

console.log(getResults());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.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.

Thank you, I finally figured it out

1 Like