Tell us what’s happening:
I’m getting stuck on the getResults function. I have tests 1-10 passed, but 11-13 are failing. I have an if/else set up for getResults. I believe the if/else is set up right, but my console.log (third one in the middle) is showing the function as undefined. I’ve researched, looked at other forum posts, I have no idea where to go from here ![]()
Your code so far
const questions = [
{
category: "cat1",
question: 'ques1?',
choices: ["q1c1", "q1c2", "q1c3"],
answer: "q1c1"
},
{
category: "cat2",
question: 'ques2?',
choices: ["q2c1", "q2c2", "q2c3"],
answer: "q2c1"
},
{
category: "cat3",
question: 'ques3?',
choices: ["q3c1", "q3c2", "q3c3"],
answer: "q3c1"
},
{
category: "cat4",
question: 'ques4?',
choices: ["q4c1", "q4c2", "q4c3"],
answer: "q4c1"
},
{
category: "cat5",
question: 'ques5?',
choices: ["q5c1", "q5c2", "q5c3"],
answer: "q5c1"
}
];
function getRandomQuestion(questions) {
let randIndex = Math.floor(Math.random() * questions.length);
return questions[randIndex]
};
let randQuestion = getRandomQuestion(questions);
let choiceArr = randQuestion.choices;
function getRandomComputerChoice(choiceArr) {
let randIndex = Math.floor(Math.random() * choiceArr.length);
return choiceArr[randIndex]
};
let randChoice = getRandomComputerChoice(choiceArr);
function getResults(randQuestion, randChoice) {
let correctAns = randQuestion.answer
if (randChoice === correctAns) {
"The computer's choice is correct!"
} else {
"The computer's choice is wrong. The correct answer is: `${correctAns}`"
}
}
console.log(randQuestion);
console.log(" break ");
console.log(randChoice);
console.log(" break ");
console.log(getResults(randQuestion, randChoice))
console.log(" break ");
console.log(`${randQuestion.answer}`)
console.log(" break ");
console.log(choiceArr)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game
https://www.freecodecamp.org/learn/full-stack-developer/lab-quiz-game/lab-quiz-game