Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I’m testing as I go, so anything past 9 isn’t of concern to me yet. However, my code performs the task that test 9 requires but the test shows me failing. Why isn’t my code passing that test?

Your code so far

const questions = [
    {
        category: "sports",
        question: "Who is the NBA GOAT?",
        choices: ["LeFlop Blames", "Michael Jordan", "Wilt Chamberlain"],
        answer: "Michael Jordan"
    },
    {
        category: "history",
        question: "What was Napoleon's last battle?",
        choices: ["Austerlitz", "Gettysburg", "Waterloo"],
        answer: "Waterloo"
    },
    {
        category: "science",
        question: "How many electrons are present in a neutral radium atom?",
        choices: ["75", "111", "88"],
        answer: "88"
    },
    {
        category: "movies",
        question: "Who played Rick Blaine in Casablanca?",
        choices: ["Humphrey Bogart", "James Cagney", "Cary Grant"],
        answer: "Humphrey Bogart"
    },
    {
        category: "music",
        question: "What famous guitarist and band leader played a famous solo on Michael Jackson's Beat It?",
        choices: ["Steve Vai", "BB King", "Eddie Van Halen"],
        answer: "Eddie Van Halen"
    }
];

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

console.log(randomQuestion)

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

const randomAnswer = getRandomComputerChoice();

console.log(randomAnswer)

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

why are you overwriting the function parameter?

The code works without a parameter at all if I declare the variable inside the function. But creating the function with the parameter is what the user story asked for. I figured it out. I had to declare the variable outside the function so I could call it as the function parameter. Seems arbitrary, since I’m only ever going to need that specific variable for that single function, and the way I wrote it would work without needing a parameter. Is there a reason that declaring that variable globally is preferred?

in one case your function is reusable, in the other is not

consider

const randomQuestion1 = getRandomQuestion(questions);
const computerChoice1 = getRandomComputerChoice(randomQuestion1.choices);

const randomQuestion2 = getRandomQuestion(questions);
const computerChoice2 = getRandomComputerChoice(randomQuestion2.choices);

would your function work?