Help Needed with "Build a Quiz Game" Lab – Test Failures Despite Correct Output

:waving_hand:t2::wink: Hello freeCodeCamp community,

I hope you’re all doing well! I’m currently working on the “Build a Quiz Game” lab from the Full Stack Developer certification, and I’m running into some challenges with passing the tests. Even though my code produces the correct output in the console, I’m failing certain test cases with my code. I’d really appreciate any advice or insights from the community to help me figure out what’s going wrong.

Code 01 ↓

const obj1 = {
    category: "JavaScript Question",
    question: "What is the correct way to declare a variable that cannot be reassigned in JavaScript?", 
    choices: ["let x = 10;", "const x = 10;", "var x = 10;"],
    answer: "const x = 10;"
};
const obj2 = {
    category: "CSS Question",
    question: "Which CSS property is used to change the text color of an element?", 
    choices: ["font-style", "color", "text-align"],
    answer: "color"
};
const obj3 = {
    category: "CSS & HTML Question",
    question: "What does the \"viewport\" meta tag in HTML primarily help with?", 
    choices: ["Setting the font size of the webpage", "Controlling the layout on mobile browsers", "Adding animations to the page"],
    answer: "Controlling the layout on mobile browsers"
};
const obj4 = {
    category: "JavaScript Question",
    question: "Which method is used to add an element to the end of an array in JavaScript?", 
    choices: ["array.push()", "array.pop()", "array.unshift()"],
    answer: "array.push()"
};
const obj5 = {
    category: "CSS RWD Question",
    question: "In a media query, what does the min-width condition target?", 
    choices: ["Screens smaller than the specified width", "Screens equal to or larger than the specified width", "Screens exactly at the specified width"],
    answer: "Screens equal to or larger than the specified width"
};
const questions = [obj1, obj2, obj3, obj4, obj5];

const getRandomQuestion = (questionsArray) => questionsArray[Math.round(Math.random()*(questionsArray.length - 1))];

let selectedQuestion;
let question;
let randomAnswer;

function getRandomComputerChoice(questionsArray) {
    selectedQuestion = questionsArray[Math.round(Math.random()*(questionsArray.length - 1))];
    question = `${String(selectedQuestion.category)} → ${String(selectedQuestion.question)}`;
    randomAnswer = (selectedQuestion.choices)[Math.round(Math.random()*((selectedQuestion.choices).length - 1))];
    return `Selected Question: ${String(question)}
Answer of the Computer: ${String(randomAnswer)}`;
}

function getResults(questionsArray) {
    console.log(getRandomComputerChoice(questionsArray));
    if (selectedQuestion.answer == randomAnswer) {
        return "The computer's choice is correct!";
    } else {
        return `The computer's choice is wrong. The correct answer is: ${String(selectedQuestion.answer)}`;
    }
}

console.log(getResults(questions));

Console Output ↓

// running tests
9. You should have a function named getRandomComputerChoice that takes the array of the available choices as a parameter, and returns a random answer to the selected question.
11. If the computer choice matches the answer, getResults should return The computer's choice is correct!
12. If the computer choice doesn't match the answer, getResults should return The computer's choice is wrong. The correct answer is: <correct-answer>, where <correct-answer> is the value of the correct answer to the chosen question.
// tests completed
// console output
Selected Question: JavaScript Question → Which method is used to add an element to the end of an array in JavaScript?
Answer of the Computer: array.pop()
The computer's choice is wrong. The correct answer is: array.push()

Thank you so much for taking the time to read this and for any help you can offer! I’m excited to get this working and move forward with the lab.

Best regards,
@HimalErangana

Hi there. You shouldn’t have that variables in global scope

1 Like

this is not only the random answer from the computer, you have added too much stuff

btw, ${String(question)} is unnecessary, the ${} already would turn what you put in it to a string

1 Like

Thank you for replying, @hasanzaib1389.

But, If I remove let selectedQuestion;let question;let randomAnswer;, I will not be able to call getResults() function, because I used some variables from getRandomComputerChoice() function. So, I had to set selectedQuestion, question & randomAnswer as global variables.

Here is what happens If I remove the part, as you suggested in your comment. ↓

[Running] node "c:\Users\Himal Erangana\Desktop\FreeCodeCamp - JS\main.js"
Selected Question: JavaScript Question → Which method is used to add an element to the end of an array in JavaScript?
Answer of the Computer: array.pop()
c:\Users\Himal Erangana\Desktop\FreeCodeCamp - JS\main.js:49
    if (selectedQuestion.answer == randomAnswer) {
    ^

ReferenceError: selectedQuestion is not defined

[Done] exited with code=1 in 0.749 seconds

What did you mean by that? I’m afraid I can’t quite understand what you’re saying. Shouldn’t the computer randomly select only one string as the random answer? :roll_eyes:

You should not use the global state to pass around information. That is what function arguments and return values are for.

yes, and that is what should be returned, one of the possible answers of the question.

is that one of the possible answers or more?

and so define them inside your function, the function should use the parameters to determine the output, not other global values

also the getRandomQuestion should get the whole array of questions as argument

You should read again the thing for getRandomComputerQuestion:

  1. You should have a function named getRandomComputerChoice that takes the array of the available choices as a parameter, and returns a random answer to the selected question.

does your function uses the array of available choices of one question as parameter?

the order of working is:

  • select a question with getRandomQuestion. Now you have a question object.
  • select a computer answer with getRandomComputerChoide from the choices in the question object
  • and then, getResults check if the computer answers correctly comparing the output of the two previous functions:
  1. You should have a function named getResults that takes the selected question object and the computer choice as its parameters and returns The computer's choice is correct! if the answer is correct. Otherwise, it returns The computer's choice is wrong. The correct answer is: <correct-answer>, where <correct-answer> is the value of the correct answer to the chosen question.