Build a Quiz Game - getResults

I assumed, we are expected that our getResults function should pick up a random answer from a random question and compare it with the answer?

If yes, here is my code that fails the test:

const questions = [];

const obj1 = {
  category: "JavaScript",
  question: "What is JavaScript?",
  choices: ["A declarative language",
              "A programming language",
              "A coffee brand"],
  answer: "A programming language",
};


const obj2 = {
  category: "HTML",
  question: "What is HTML?",
  choices: ["I have no clue", 
            "Hyper Text Markup Language",
            "Some programming language"],
  answer: "Hyper Text Markup Language",
};


const obj3 = {
  category: "CSS",
  question: "What is CSS?",
  choices: ["Name of a company",
              "Some programming language",
              "Cascading Style Sheets"],
  answer: "Cascading Style Sheets",
};


const obj4 = {
  category: "Development Tools",
  question: "What is VSCode?",
  choices: [
    "Development IDE",
    "A coding editor",
    "A coding debugger"],
  answer: "A coding editor",
};

const obj5 = {
  category: "Soccer",
  question: "Who is David Beckham",
  choices: [
    "Hollywood actor",
    "A pop singer",
    "Former England Team Captain"],
  answer: "Former England Team Captain",
};

questions.push(obj1, obj2, obj3, obj4, obj5);

function getRandomQuestion(questions) {
  const randomIndex = Math.floor(Math.random() * questions.length);
  return questions[randomIndex];
}

function getRandomComputerChoice(choices) {
  const numberOfChoices = choices.length;
  const randomIndex = Math.floor(Math.random() * numberOfChoices);
  return choices[randomIndex];
}

function getResults(questions, choices) {

const randomIndexQuestion = Math.floor(Math.random() * questions.length);

const randomQuestion = questions[randomIndexQuestion];
  
const numberOfChoices = questions[randomIndexQuestion].choices.length;

const randomChoiceIndex = Math.floor(Math.random() * numberOfChoices);

const computerChoice = questions[randomIndexQuestion].choices[randomChoiceIndex];

if (questions[randomIndexQuestion].choices[randomChoiceIndex] != computerChoice) {

 console.log(`The computer's choice is wrong. The correct answer is: ${questions[randomIndexQuestion].answer} `)
  } else {
   console.log("The computer choice is correct!");     
  }
}

I have a feeling it may have something to do with the choices argument in the function. For some reasons it is greyed out.

Any help will be much appreciated.

Hi @wpdevstudent

Please provide a link to the challenge.

Happy coding

> Here

is the link.

You need to place a certain symbol after a question.
You did this for the first lot.

Are you saying there is a syntax error in the function?

Sorted.

const obj5 = {
  category: "Soccer",
  question: "Who is David Beckham?",
  choices: [
    "Hollywood actor",
    "A pop singer",
    "Former England Team Captain"],
  answer: "Former England Team Captain",
};

Next hint will be much appreciated.

I figured it out. It was all in understanding the function requirements properly.

1 Like

good job! understanding the requirements is one of the hard parts