JavaScript Objects: Build a Quiz Lab

I recently passed this topic, but I had to delete these two variable definitions in order to pass user tests 11, 12, and 13.

    1. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.
  • Passed:12. If the computer choice matches the answer, getResults should return The computer's choice is correct!

  • Passed:13. 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.

I am not sure I am understanding why this does not work, especially for being able to test/log results in one call console.log(getResults()). I am not sure I see how the data would pass through the functions otherwise to ensure getRandomQuestion() isn’t called twice, other than explicitly setting the outputs outside of the functions when calling them. Sometimes I am not sure how the testing works and wish it would be more explicit.

const getResults = (question, computerChoice) => {
    question = getRandomQuestion(questions);
    // console.log(question);
    computerChoice = getRandomComputerChoice(question.choices);
    // console.log(computerChoice);
    // console.log(question.answer);
    

Additionally, originally, I also had this code for getting the computer’s choice, which was incorrect. I see the user story asks for the list of available choices, but I was thinking of it as passing the data of the random question call to the computer choice function. Is this the correct way to be thinking about functions or am I not abstracting enough/thinking about creating programs the right way?

const getRandomComputerChoice = (questions) => {
    let computerChoice = questions.choices[Math.floor(Math.random() * (questions.choices.length))];
    return computerChoice;
}

My full code is blurred and included below as well. Thanks!

const questions = [
    {
        category: "one",
        question: "two?",
        choices: ["three", "six", "four"],
        answer: "four"
    },
    {
        category: "cat",
        question: "dog?",
        choices: ["house", "dog", "pig"],
        answer: "pig"
    },
    {
        category: "hat",
        question: "coat?",
        choices: ["hat", "coat", "skirt"],
        answer: "skirt"
    },
    {
        category: "water",
        question: "soda?",
        choices: ["water", "soda", "beer"],
        answer: "beer"
    },
    {
        category: "plant",
        question: "stick?",
        choices: ["bag", "plant", "tree"],
        answer: "tree"
    }
];

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

const getRandomComputerChoice = (choices) => {
    let computerChoice = choices[Math.floor(Math.random() * choices.length)];
};

const getResults = (question, computerChoice) => {
    question = getRandomQuestion(questions);
    console.log(question);
    computerChoice = getRandomComputerChoice(question.choices);
    console.log(computerChoice);
    console.log(question.answer);
    
    if (computerChoice === question.answer) {
        return "The computer's choice is correct!";
    } else {
        return "The computer's choice is wrong. The correct answer is: " + question.answer;
    }
}

getResults function is expected to only determine the result based on the passed in question and computerChoice.

Then the example of intended usage would be the following:

const question = getRandomQuestion(questions);
console.log("Question:", question.question);
const computerChoice = getRandomComputerChoice(question.choices);
console.log("Computer choice:", computerChoice);

const result = getResults(question, computerChoice);
console.log("Result:", result);

Passing the data happens only with the arguments function takes and what it returns.

Did you notice there was no return statement in the getRandomComputerChoice() function? So, I don’t see how @aliensprout could have passed this challenge. Also, I’m pretty sure the practice of assigning values to function parameters inside the function is an anti-pattern.