Build a Quiz Game - Problem in getResults()

Tell us what’s happening:

My code cannot pass item 9 (getResults function). I checked my code’s output by console.log() so I think it’s work properly.

Your code so far

/* Questions Bank*/

const questions = [
{
  category: "q1 cate",
  question: "q1 ques?",
  choices: ["q1c1", "q1c2", "q1c3"],
  answer: "q1c1"
},
{
  category: "q2 cate",
  question: "q2 ques?",
  choices: ["q2c1", "q2c2", "q2c3"],
  answer: "q2c2"
},
{
  category: "q3 cate",
  question: "q3 ques?",
  choices: ["q3c1", "q3c2", "q3c3"],
  answer: "q3c3"
},
{
  category: "q4 cate",
  question: "q4 ques?",
  choices: ["q4c1", "q4c2", "q4c3"],
  answer: "q4c1"
},
{
  category: "q5 cate",
  question: "q5 ques?",
  choices: ["q5c1", "q5c2", "q5c3"],
  answer: "q5c2"
}
];

/* Random numbers creation*/
let rndQuestionNum = Math.floor(Math.random() * 5);
let rndChoiceNum = Math.floor(Math.random() * 2);

/* Functions */
function getRandomQuestion(questions) {
return questions[rndQuestionNum];
};


function getRandomComputerChoice(choices) {
return choices[rndChoiceNum];
};


function getResults(rndQuestion, pcChoice) {
  if (pcChoice === rndQuestion) {
return `The computer's choice is correct!`
}

else {
return `The computer's choice is wrong. The correct answer is: ${rndQuestion}`
};

};


/* Test code */
console.log("Selected question:");
console.log(getRandomQuestion(questions));
console.log("");
console.log("Selected choice:");
console.log(getRandomComputerChoice(questions[rndQuestionNum].choices));
console.log("\n Result:");
console.log
(
  getResults(
    getRandomQuestion(questions).answer, 
    getRandomComputerChoice(questions  [rndQuestionNum].choices)
            )
)

/* End */


Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Welcome to the forum @Afshin

    1. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.

The first parameter should be an object.

Happy coding

1 Like

Thank to your fast reply :slight_smile:
Is it your mean:

if (pcChoice === rndQuestion.answer) 

here you are not calling it with an object

1 Like

Thanks again;
I edited my code (getResults function) and the problem was solved. :smiling_face_with_three_hearts:

The problem was that I wasn’t modifying the object name in the “else” section.

function getResults(rndQuestion, pcChoice) {
  if (pcChoice === rndQuestion.answer) {
return `The computer's choice is correct!`
}

else {
return `The computer's choice is wrong. The correct answer is: ${rndQuestion.answer}`
};

};