Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Hello, I’m having trouble with identifying what is not letting me pass the tests, the output seems to be working for each function, I’d like some help please.

Your code so far

let questions = [];

const q1 = {
  category: "singer" ,
  question: "Who sang the title song for the latest Bond film, No Time to Die?" ,
  choices: ["Adele", "Sam Smith", "Billie Eilish"] ,
  answer: "Billie Eilish"
};

const q2 = {
  category: "Country" ,
  question: "Which flies a green, white, and orange (in that order) tricolor flag?" ,
  choices: ["Ireland", "Ivory Coast", "Italy"] ,
  answer: "Ireland"
};

const q3 = {
  category: "Smartphone Maker" ,
  question: "What company makes the Xperia model of smartphone?" ,
  choices: ["Samsung", "Sony", "Nokia"] ,
  answer: "Sony"
};

const q4 = {
  category: "Famous place" ,
  question: "Which city is home to the Brandenburg Gate?" ,
  choices: ["Vienna", "Zurich", "Berlin"] ,
  answer: "Berlin"
};

const q5 = {
  category: "Fruit" ,
  question: "Which of the following is NOT a fruit?" ,
  choices: ["Rhubarb", "Tomato", "Avocado"] ,
  answer: "Rhubarb"
};

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

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

console.log(getRandomQuestion(questions))

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

console.log(getRandomComputerChoice(q5.choices));

let rq = getRandomQuestion(questions);
let rcc = getRandomComputerChoice(rq.choices);

function getResults(qobj, cchoice) {
 if(qobj.answer === cchoice) {
   return "The computer's choice is correct!";
 }
 else if (rq.answer !== rcc) {
   return "The computer's choice is wrong. The correct answer is: " + rq.answer;
 }
}



console.log(rq);
console.log(rcc);
console.log(getResults(rq, rcc.choices));

console.log(typeof(rq))
console.log(typeof(rcc))


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Updated the code and removed .choices at the end of console.log(getResults(rq, rcc.choices)):

let questions = [];

const q1 = {
  category: "singer" ,
  question: "Who sang the title song for the latest Bond film, No Time to Die?" ,
  choices: ["Adele", "Sam Smith", "Billie Eilish"] ,
  answer: "Billie Eilish"
};

const q2 = {
  category: "Country" ,
  question: "Which flies a green, white, and orange (in that order) tricolor flag?" ,
  choices: ["Ireland", "Ivory Coast", "Italy"] ,
  answer: "Ireland"
};

const q3 = {
  category: "Smartphone Maker" ,
  question: "What company makes the Xperia model of smartphone?" ,
  choices: ["Samsung", "Sony", "Nokia"] ,
  answer: "Sony"
};

const q4 = {
  category: "Famous place" ,
  question: "Which city is home to the Brandenburg Gate?" ,
  choices: ["Vienna", "Zurich", "Berlin"] ,
  answer: "Berlin"
};

const q5 = {
  category: "Fruit" ,
  question: "Which of the following is NOT a fruit?" ,
  choices: ["Rhubarb", "Tomato", "Avocado"] ,
  answer: "Rhubarb"
};

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

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

console.log(getRandomQuestion(questions))

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

console.log(getRandomComputerChoice(q5.choices));



function getResults(qobj, cchoice) {
 if(qobj.answer === cchoice) {
   return "The computer's choice is correct!";
 }
 else if (rq.answer !== rcc) {
   return "The computer's choice is wrong. The correct answer is: " + rq.answer;
 }
}
          
let rq = getRandomQuestion(questions);
let rcc = getRandomComputerChoice(rq.choices);

console.log(rq);
console.log(rcc);
console.log(getResults(rq, rcc));
              
console.log(typeof(rq))
console.log(typeof(rcc))


test 11 seems to pass sometimes but not test 13 and 14

Got it working referenced the global scope variable name (rq) for the else if statement instead of the parameter name (qobj) which was incorrect


function getResults(qobj, cchoice) {
 if(qobj.answer === cchoice) {
   return "The computer's choice is correct!";
 }
 else if (qobj.answer !== rcc) {
   return "The computer's choice is wrong. The correct answer is: " + qobj.answer;
 }
}

You should not refer to a global variable inside your function. Only use the parameters passed to the function.

The only reason this code passed the tests is because it’s redundant. You’ve already established that the answer does not equal the computer choice when your code gets to the else.

1 Like