Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I am using

function getRandomComputerChoice(arr){
  //get the choices randomly from array
  let choices = arr[Math.random() * arr.length]
  return choices;
}

and I am getting random object from the array., but I was expecting it to give me was a floating number as Math.random() gives you anything between 0 and 1 (excluding). Why I am getting the whole random object instead of undefined.

Your code so far

const questions = [
  {
    category: 'favorite',
    question: 'What is your favorite colour?',
    choices: ['blue','white','black'],
    answer: 'white'
  },
    {
    category: 'Food',
    question: 'What is your favorite food?',
    choices: ['Pasta','pizza','biryani'],
    answer: 'pizza'
  },
    {
    category: 'games',
    question: 'What is your favorite game?',
    choices: ['rugby','horse riding','swimming'],
    answer: 'swimming'
  },
    {
    category: 'airline',
    question: 'What is your favorite airline?',
    choices: ['airblue','emirates','etihad'],
    answer: 'etihad'
  },
    {
    category: 'time of day',
    question: 'What is your favorite time of day?',
    choices: ['morning','evening','night'],
    answer: 'morning'
  }
]
function getRandomQuestion(arr){
  let questionObj = arr[Math.floor(Math.random() * arr.length)]
  return questionObj
}
function getRandomComputerChoice(arr){
  //get the choices randomly from array
  let choices = arr[Math.random() * arr.length]
  return choices;
}
console.log(getRandomQuestion(questions))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-quiz-game/66f17db06803d11a1bd19a20.md at main · freeCodeCamp/freeCodeCamp · GitHub

what is the code testing getRandomComputerChoice?

Hi @mahassan,

Where are you calling getRandomComputerChoice to test it? What are you passing to it? Why would you ever want an index to be a float?

Happy coding

I provided it a object and a random index number. I expected it to give me undefined because Math.random() mostly give a float.

Because ``Math.random()` give you a float most of the time.

I am providing the object questions

Indexes are always integers. You need to apply another method to turn the float into an integer.

How is that sending in an array of choices? Where should you be getting choices from to see if the computer’s choice matches the answer in the question object?

Ok, to cut down to basic, here is my code and start of problem

const questions = [
  {
    category: 'favorite',
    question: 'What is your favorite colour?',
    choices: ['blue','white','black'],
    answer: 'white'
  },
    {
    category: 'Food',
    question: 'What is your favorite food?',
    choices: ['Pasta','pizza','biryani'],
    answer: 'pizza'
  },
    {
    category: 'games',
    question: 'What is your favorite game?',
    choices: ['rugby','horse riding','swimming'],
    answer: 'swimming'
  },
    {
    category: 'airline',
    question: 'What is your favorite airline?',
    choices: ['airblue','emirates','etihad'],
    answer: 'etihad'
  },
    {
    category: 'time of day',
    question: 'What is your favorite time of day?',
    choices: ['morning','evening','night'],
    answer: 'morning'
  }
]

function getRandomComputerChoice(arr){
  //get the choices randomly from array
  let choicex = arr[1].choices
  return choicex
}
console.log(getRandomQuestion(questions))

I am asking for array 1 and then key of choices but it is giving me output of whole array at position 1. I tried doing the bracket let choicex = arr[1]['choices'] but that didn’t work as well (meaning it is giving me whlle object at that position.

Edit: I found my mistake. I was asking for getRandomQuestion not getRandomComputerChoice so ignore this post.

I got my code working, but for some random ones it gives me undefined. why?

function getRandomComputerChoice(arr){
  //get the choices randomly from array
  let choicex = arr[Math.floor(Math.random()* arr.length)].choices[Math.floor(Math.random()* arr.length)];
  return choicex;
}

Edit: I thought it might be because items vs array and array is one less because it starts with 0, but I still getting undefined

It’s because you are always using the length of the questions array to get the random number (an index) even when it’s applied to the choices array, which does not have the same length as the questions array.

That said, you should be passing in the choices array from the randomly selected question object; otherwise, how can you determine if the computer’s choice matches the answer to the randomly selected question object when you implement getResults?

I did not understand what you mean here.

Please review User Story #9. The getResults function is the goal of this challenge:

  • get a random question object (passed in as the first parameter)
  • get a random choice from that object to represent the computer’s choice (passed in as the second parameter)
  • compare the question object’s answer to the computer’s choice and return a “correct or wrong” message

Happy coding

I am more confused because you are talking about US#9 whereas I am talking about 8.

  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.

and I am getting undefined sometimes.

You can’t just say “I am confused” because it does not give enough information to answer you.

You should follow the advice given and if you have a question about it, please ask. We need to know what it is that is confusing you.