Tell us what’s happening:
How do I progress further, please help. Give me some hint, guide me little bit. Also in my getComputerChoice function I am unable to use parameter
Your code so far
const questions = [
{
category: "PCo",
question: "fav color ?",
choices: ['Red', 'Black', 'Both'],
answer: 'Red'
},
{
category: "PCa",
question: "Fav car ?",
choices: ['Honda', 'BMW', 'both'],
answer: 'Honda'
},
{
category: "PB",
question: "Fav brand ?",
choices: ['Amazon', 'Google', 'Both'],
answer: 'Both'
},
{
category: "PF",
question: "Fav fruit ?",
choices: ['Apple', 'Grapes', 'Mango'],
answer: 'Mango'
},
{
category: "PH",
question: "fav hobby ?",
choices: ['Singing', 'Coding', 'Both'],
answer: 'Both'
}
];
let randomNum = Math.floor(Math.random() * 5);
let anotherRandomNum = Math.floor(Math.random() * 3);
function getRandomQuestion() {
return questions[randomNum].question
}
function getRandomComputerChoice() {
return questions[randomNum].choices[anotherRandomNum];
}
function getResults() {}
console.log(getRandomQuestion())
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/131.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game
Don’t make the random assignment globally.
Write the logic within the functions. Parameters Geos within the ()
braces of function definition.
but then how do I get same answer choices of that same question. I will show you an example.
const questions = [
{
category: "PCo",
question: "fav color ?",
choices: ['Red', 'Black', 'Both'],
answer: 'Red'
},
{
category: "PCa",
question: "Fav car ?",
choices: ['Honda', 'BMW', 'both'],
answer: 'Honda'
},
{
category: "PB",
question: "Fav brand ?",
choices: ['Amazon', 'Google', 'Both'],
answer: 'Both'
},
{
category: "PF",
question: "Fav fruit ?",
choices: ['Apple', 'Grapes', 'Mango'],
answer: 'Mango'
},
{
category: "PH",
question: "fav hobby ?",
choices: ['Singing', 'Coding', 'Both'],
answer: 'Both'
}
];
function getRandomQuestion() {
let randomNum = Math.floor(Math.random() * 5);
return questions[randomNum].question
}
function getRandomComputerChoice() {
let randomNum = Math.floor(Math.random() * 5);
let anotherRandomNum = Math.floor(Math.random() * 3);
return questions[randomNum].choices[anotherRandomNum];
}
function getResults() {}
console.log(getRandomQuestion())
console.log(getRandomComputerChoice())
ILM
January 22, 2025, 4:15pm
5
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
ILM
January 22, 2025, 4:16pm
6
getRandomQuestion
should return an object, not a string
Yes, you should return a random object form the questions
array.
Also you need to return rendom choices from the choices
array.
Hint: to get a random index of an array, you need to floor the array length and return that index in the array.
but how ? 4th point in lab says - The question
key should have the value of a string representing a question.
it is a string, how do I make it return object ?
ILM
January 22, 2025, 7:20pm
9
but the question key is inside… what?
inside an object inside questions array
ILM
January 22, 2025, 7:21pm
11
so you have an object you can return
yes, but I am not able to think how.
Can you please guide me step by step ?
ILM
January 22, 2025, 7:23pm
13
here, are you retrieving the question from what?
ILM
January 22, 2025, 7:23pm
15
but inside the array what do you have?
5 objects
so, I am retrieving the question from objects elements inside questions array
ILM
January 22, 2025, 7:25pm
17
so you are getting the question from one specific object, right? what if you just don’t get the question from the object?
I just get an random object
ILM
January 22, 2025, 7:26pm
19
and is there something wrong with that?
No, i can’t think what is wrong and why it is wrong
ILM
January 22, 2025, 7:28pm
21
try it then, try it in the code
1 Like