Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

iam trying to redo the lab for practicing but i failed to pass test number 9 although i get good results in console !
if someone could gimme a hint or tips and tricks so i can build up my coding muscles :slight_smile:

Your code so far

const questions = [
  {
    category: 'Sports',
    question: 'Who is the greatest athlete ever ?',
    choices: ['M.Tricka', 'El-khateeb', 'H.Shehata'],
    answer: 'M.Tricka',
  },
  {
    category: 'Policy',
    question: 'Who is the greatest athlete ever ?',
    choices: ['M.Trickai', 'El-khateebi', 'H.Shehatai'],
    answer: 'M.Trickai',
  },
  {
    category: 'Crime',
    question: 'Who is the greatest athlete ever ?',
    choices: ['M.Trickaii', 'El-khateebii', 'H.Shehataii'],
    answer: 'M.Trickaii',
  },
  {
    category: 'Cinema',
    question: 'Who is the greatest athlete ever ?',
    choices: ['M.Trickaiii', 'El-khateebiii', 'H.Shehataiii'],
    answer: 'M.Trickaiii',
  },
  {   
    category: 'SportsX',
    question: 'Who is the greatest athlete ever ?',
    choices: ['M.Trickaxi', 'El-khateebxi', 'H.Shehataxi'],
    answer: 'M.Trickaxi',
  },
]

function getRandomQuestion(arr) {
  let randomIndex = Math.floor(Math.random() * arr.length)

  return arr[randomIndex]
}

function getRandomComputerChoice(arr) {
     arr = getRandomQuestion(questions).choices;
    let randomIndex = Math.floor(Math.random() * 3)

    return arr[randomIndex]

}

// console.log(questions)            

         
console.log(getRandomComputerChoice())                                   

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

https://www.freecodecamp.org/learn/full-stack-developer/lab-quiz-game/lab-quiz-game

You are assigning a value to a parameter passed to the function. That’s an anti-pattern.

um anti pattern so you mean this is a bad use of parameter in total or just here in the event ?

In general, it’s bad practice to reassign a function parameter. If you need to use the param and intend to make a change to its value, it’s best to create a new variable for that.

For example:

function myFunc(param) {
  let myNbr = param * 2;
  return myNbr;
}

But it’s bad practice to do this:

function myFunc(param) {
  param = newValue;
  ...
}

In getRandomComputerChoice(), you should pass in an array of choices from a question.

1 Like