What am I doing wrong that my function isn’t working right? My first 2 function aren’t passing and I am looking at a hint of where I am missing/going wrong, spend like 2 hours on it put I am confused.
Your code so far
const questions = [
{
category: 'Tech',
question: 'What is most popular phone now a days?',
choices: ['blackberry','iPhone','nokia'],
answer: 'iPhone'
},
{
category: 'Food',
question: 'What is most popular food in Pakistan?',
choices: ['Plain rice','Biryani','Korma'],
answer: 'Biryani'
},
{
category: 'Takeaway',
question: 'What is most popular takeaway?',
choices: ['Philly','Burgerism','Grubbies'],
answer: 'Philly'
},
{
category: 'Airlines',
question: 'What is most popular airlines?',
choices: ['Emirates','Etihad','Qatar'],
answer: 'Emirates'
},
{
category: 'Travel',
question: 'What is most popular SVU?',
choices: ['Toyota','Kia','JLR'],
answer: 'Kia'
},
]
function getRandomQuestion(arr){
return arr[Math.floor(Math.random() * arr.length)].question
}
function getRandomComputerChoice(answerArr){
return answerArr[Math.floor(Math.random() * answerArr.length).answer]
}
function getResults(arr, answer){
if(arr.question !== answer){
return `The computer's choice is wrong. The correct answer is ${answer}`;
}else{
return "The computer's choice is correct!";
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
You should have a function named getRandomQuestion that takes an array of questions as a parameter and returns a random question object from the array.
The confusing part is last bit where it says “returns a random question object from the array.” So not a question but the whole object as question itself is a key and its value is a string.
function getRandomComputerChoice(arr){
return arr[Math.floor(Math.random() * answerArr.length)].answer;
}
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.
What am I missing, when running the code on my machine, I do have a random answer from choices, then what am I missing?
function getRandomComputerChoice(answerArr){
return answerArr[Math.floor(Math.random() * answerArr.length)].choices[Math.floor(Math.random())];
}
But doing that doesn’t fix the error, but it gives me a value from array of choices, but the user stories says ‘answer’ but here we are outputting choices
but in the function body, I am accessing the array of choices when I do console.log(answerArr[Math.floor(Math.random() * answerArr.length)].choices[Math.floor(Math.random())]);
no, you can’t change how the function works, the function need to work receiving the array of choices of a specific question, not the array of questions