Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Code works fine except 11, 12, and 13[ basically the getResult function]. I don’t know what I’m doing wrong, any help will be greatly appreciated

Your code so far

let questions = [
  {
    category : "history",
    question : "Alexander the great is from?",
    choices : ["Macedonia", "Greece", "Egypt"] ,
    answer : "Macedonia"
  },
  {
    category : "maths",
    question : "2 x 2?",
    choices : ["2", "4", "6"] ,
    answer : "4",
  },
  {
    category : "physics",
    question : "Impluse formular?",
    choices : ["lb", "ma", "ft"] ,
    answer : "ft"
  },
  {
    category : "English",
    question : "Ada's gender?",
    choices : ["Girl", "Man", "Boy"] ,
    answer : "Girl"
  },
  {
    category : "chemistry",
    question : "Water compound?",
    choices : ["H2So4", "H2O", "Pb"] ,
    answer : "H2O"
  }
];

let qArr = [];
qArr.push(questions[0].question, questions[1].question, questions[2].question, questions[3].question, questions[4].question);

//console.log(qArr)
let randQ = Math.floor(Math.random() * questions.length);

//console.log(randQ)
const getRandomQuestion = (qArr) => {
  let randQFun = qArr[randQ]
  return randQFun
};

//console.log(getRandomQuestion(qArr));
let cArr = [];
cArr.push(questions[0].choices, questions[1].choices, questions[2].choices, questions[3].choices, questions[4].choices);

//console.log(cArr);
const getRandomComputerChoice = (cArr) => {
  let randCFun = cArr[randQ];
  return randCFun;
};

//console.log(getRandomComputerChoice(cArr))
//let rQuestion = questions[randQ]

//console.log(rAnswer);
//let rChoice = cArr[randQ];

//console.log(rChoice)
const getResults = (qObj, cChoice) => {
  qObj = questions[randQ];
  cChoice = qObj.choices[randQ];
  rAnswer = qObj.answer;
  if (cChoice == rAnswer){
    return "The computer's choice is correct!"
  } else{
    return "The computer's choice is wrong. The correct answer is: " + rAnswer
  }
}

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 Edg/139.0.0.0

Challenge Information:

Build a Quiz Game - Build a Quiz Game
https://www.freecodecamp.org/learn/full-stack-developer/lab-quiz-game/lab-quiz-game

Hi @ayiduisaac

You have two functions that both reference randQ, so that may not result in a random choice.

Happy coding

1 Like

why are you overwriting the function parameters?

Thank you, already fixed that. Code works well. Thank you sm.

1 Like