Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

i can’t understand why is my program has an error on test 13. if you can help me i would be thankful.

Your code so far

const questions = [] 

const questionOne={
    category:"movie",
    question:"what is faezeh's favorit movie?",
    choices:["got","the100","walking dead"],
    answer:"got",
  }
const questionTwo={
    category:"food",
    question:"what is faezeh's favorit food?",
    choices:["dolme","pizza","sushi"],
    answer:"sushi",
  }
const questionThree={
    category:"sport",
    question:"what is faezeh's favorit sport?",
    choices:["tkd","bodybuilding","footbal"],
    answer:"tkd",
  }
const questionFour={
    category:"friend",
    question:"what is faezeh's favorit friend?",
    choices:["zahra","sogand","nikoo"],
    answer:"zahra",
  }
const questionFive={
    category:"music band",
    question:"what is faezeh's favorit music band?",
    choices:["nirvana","bts","palay royals"],
    answer:"palay royals",
  } 

questions.push(questionOne,questionTwo,questionThree,questionFour,questionFive);
// console.log(questions)

//7
function getRandomQuestion(arr){
  const randomArray= arr[Math.floor(Math.random()*arr.length)]
  return randomArray;
}
//8
function getRandomComputerChoice(choicesArr){
  const randomArray= choicesArr[Math.floor(Math.random()*choicesArr.length)]
  return randomArray;
}
// console.log(getRandomComputerChoice(questionFive.choices))

//9
function getResults(question,pcChoice){
  if(question.category==="music band" && pcChoice==="palay royals") return "The computer's choice is correct!"
  else if (question.category==="music band" && pcChoice!=="palay royals") return `The computer's choice is wrong. The correct answer is: ${question.answer}`

  else if ((question.category==="friend" && pcChoice==="zahra"))return "The computer's choice is correct!"
  else if (question.category==="friend" && pcChoice!=="zahra") return `The computer's choice is wrong. The correct answer is: ${question.answer}`

  else if ((question.category==="sport" && pcChoice==="tkd"))return "The computer's choice is correct!"
  else if (question.category==="sport" && pcChoice!=="tkd") return `The computer's choice is wrong. The correct answer is: ${question.answer}`

  else if ((question.category==="food" && pcChoice==="sushi"))return "The computer's choice is correct!"
  else if (question.category==="food" && pcChoice!=="sushi") return `The computer's choice is wrong. The correct answer is: ${question.answer}`

  else if ((question.category==="movie" && pcChoice==="got"))return "The computer's choice is correct!"
   else if (question.category==="movie" && pcChoice!=="got") return `The computer's choice is wrong. The correct answer is: ${question.answer}`
}

console.log(getResults(getRandomQuestion(questions),getRandomComputerChoice(getRandomQuestion(questions).choices)))
// console.log(getResults(getRandomQuestion(questions),getRandomComputerChoice(getRandomQuestion(questions).choices)))
// console.log(getResults(getRandomQuestion(questions),getRandomComputerChoice(getRandomQuestion(questions).choices)))
// console.log(getResults(getRandomQuestion(questions),getRandomComputerChoice(getRandomQuestion(questions).choices)))
// console.log(getResults(getRandomQuestion(questions),getRandomComputerChoice(getRandomQuestion(questions).choices)))


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0

Challenge Information:

Build a Quiz Game - Build a Quiz Game

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-quiz-game/66f17db06803d11a1bd19a20.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hey @faezehhzz

You are hard-coding the answers and categories in your function logic. You should have a generic code which would work with any questions not only the one you are using.

What I am trying to say is that your code should be able to take any questions array, not only yours, and give the write answer.

For eg:

const questions = [
    {
        category: "science",
        question: "What is the chemical symbol for potassium?",
        choices: ["P", "K", "Pt"],
        answer: "K"
    },
    {
        category: "science",
        question: "What is the unit of electrical resistance?",
        choices: ["Ohm", "Coulomb", "Sievert"],
        answer: "Ohm"
    },
    {
        category: "geography",
        question: "What is the capital city of Australia?",
        choices: ["Sidney", "Canberra", "Wellington"],
        answer: "Canberra"
    },
    {
        category: "literature",
        question: 'Who wrote "1984"?',
        choices: ["Ray Bradbury", "Aldous Huxley", "George Orwell"],
        answer: "George Orwell"
    },
    {
        category: "sport",
        question: "How many players are on a standard volleyball team?",
        choices: ["6", "7", "12"],
        answer: "6"
    }
]

Your code should be able to take the above array and run it without any issues, but if we check your current code, since its hardcoded to your questions array, it won’t be able to handle the above questions array.

Hi @faezehhzz,

Your getResults function has a question parameter that should represent a question object returned from getRandomQuestion and a pcChoice parameter that should represent a random choice from the random question object’s choices array passed to getRandomComputerChoice, so why are you jumping through hoops looking at each question object in the global questions array? You already know what the question object is.

Happy coding