Tell us what’s happening:
This seems to be working on VSCode, but I can make my functions work here. It keeps coming back with an “x” regardless of how I write the the functions. I’ve even tried removing the “index” in the results but still the same. ![]()
Your code so far
const questions = []
const q1 = {
category:"Animals",
question: "Which of you pets like to purr?",
choices:["cat", "dog", "bird","hamster"],
answer:"cat"
}
const q2 = {
category:"Animals",
question: "Which of you pets like to bark?",
choices:["dog", "cat", "bird","hamster"],
answer:"dog"
}
const q3 = {
category:"Food",
question: "Which vegetable is orange and grows from the ground?",
choices:["carrots", "peas", "potatoes","lettuce"],
answer:"carrots"
}
const q4 = {
category:"People",
question: "Who can make you feel better when you're sick?",
choices:["doctor", "police", "teacher","farmer"],
answer:"doctor"
}
const q5 = {
category:"Places",
question: "Where do you go to swim in the ocean and play on the sand?",
choices:["beach", "school", "clinic","park"],
answer:"beach"
}
questions.push(q1, q2, q3, q4,q5);
function getRandomQuestion (questionArr) {
const index = Math.floor(Math.random() * questions.length)
const randomQuestion = questions[index].question
return {randomQuestion, index}
}
const {randomQuestion, index:questionIndex} = getRandomQuestion(questions);
function getRandomComputerChoice (questionArr, questionIndex) {
const answerIndex = Math.floor(Math.random() * questionArr[questionIndex].choices.length)
return questions[questionIndex].choices[answerIndex]
}
const correctAnswer = questions[questionIndex].answer
const computerAnswer = getRandomComputerChoice(questions,questionIndex)
function getResults (computerAnswer,correctAnswer) {
if (computerAnswer !== correctAnswer) {
return "The computer's answer is wrong. The correct answer is:" + correctAnswer;
} else {
return "The computer's answer is correct!";
}
}
result = getResults(computerAnswer,correctAnswer)
console.log (randomQuestion)
console.log ("The computer's answer is:" + computerAnswer)
console.log ("The correct answer is:" + correctAnswer)
console.log (result)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.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