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
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 am more confused because you are talking about US#9 whereas I am talking about 8.
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.