Build a Quiz Game - Build a Quiz Game

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. :frowning:

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

Hi @jaylearnsprogramming and welcome to our community!

You need to use a keyword to declare a variable in JavaScript:

Other than that, which tests are failing for you and what have you tried to do to address the failing tests?

I updated my code, but I’m still failing steps 11 to 13. It runs fine, but the test environment seems to think I did something wrong

here’s my updated code after defining all the objects:

questions.push(q1, q2, q3, q4,q5);

function getRandomQuestion (questions) {
  const index = Math.floor(Math.random() * questions.length);
  const randomQuestionSet = questions[index];
  return randomQuestionSet;
}
const randomObject = getRandomQuestion (questions);
const randomQuestion = randomObject.question;
const questionIndex = questions.findIndex (questions => questions.question === randomQuestion);
const availableChoiceArr = questions[questionIndex].choices

function getRandomComputerChoice (availableChoiceArr) {
  const index = Math.floor(Math.random() * availableChoiceArr.length);
  return availableChoiceArr[index]
}
const computersChoice = getRandomComputerChoice (availableChoiceArr)

function getResults (randomObject,computersChoice) {
  const correctAns = randomObject.answer
  if (computersChoice !== correctAns) {
    return "The computer's answer is wrong. The correct answer is:" + correctAns;
  } else {
    return "The computer's answer is correct!";
  }
}

console.log (randomQuestion)
console.log ("The computer's answer is:" + computersChoice)
console.log ("The correct answer is:" + randomObject.answer)
console.log (getResults (randomObject,computersChoice) )```
1 Like

Welcome to the forum @jaylearnsprogramming

Where did you define the questions array?

Happy coding

2 Likes

I defined 4 variables before getting the random choices and results:

const randomObject = getRandomQuestion (questions);
const randomQuestion = randomObject.question;
const questionIndex = questions.findIndex (questions => questions.question === randomQuestion);
const availableChoiceArr = questions[questionIndex].choices

that does not answer the question about where the questions array is defined

Sorry about that. But I got the answer.
I converted result of getRandomQuestion (questions); to an Object().

thanks for all the help and brainstorming :slightly_smiling_face:

2 Likes

you do not need to convert anything here, the thing to return is already an object