Tell us what’s happening:
I am unsure why step 8 is failing. My output and type are correct. My steps 9-14 are unfinished.
Your code so far
const questions = [
{
category: "Science",
question: "Who?",
choices: ["Here", "There", "Everywhere"],
answer: "Here",
},
{
category: "Tech",
question: "Where?",
choices: ["Here", "There", "Everywhere"],
answer: "There"
},
{
category: "Engineering",
question: "When?",
choices: ["Here", "There", "Everywhere"],
answer: "Everywhere",
},
{
category: "Math",
question: "What?",
choices: ["Here", "There", "Everywhere"],
answer: "Here",
},
{
category: "Arts",
question: "Why?",
choices: ["Here", "There", "Everywhere"],
answer: "There",
}
];
function getRandomQuestion() {
let randomNum = Math.floor(Math.random() * 5);
const objectQ = [questions[randomNum].question];
return objectQ;
}
let randQuest = getRandomQuestion();
console.log(randQuest);
const newCarr =
function getRandomComputerChoice() {
let randomNum = Math.floor(Math.random() * 3);
const objectC = [];
return
}
function getResults(question, choices) {
const choiceArr = [questions[randomNum].choices];
const answerArr = [questions[randomNum].answer];
if (choiceArr.includes(answerArr)) {
return "The computer's choice is correct!";
} else {
return "The computer's choice is wrong.";
}
return
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game
I recommend to implement based on the User Stories, not the tests.
- You should have a function named
getRandomQuestion that takes an array of questions as a parameter
- You should have a function named
getRandomQuestion that takes an array of questions as a parameter
I’m totally stumped, I’m not sure hwo to proceed with User Story 7 considering my output is correct. I tried placing the questions array inside of the getRandomQuestion function parameter and I’m still getting an error for test 8
Output is one thing. There are many ways to write a program but you must implement this one exactly as instructed regarding the parameters to pass the tests.
Please share your updated code?
const qArr = [questions.question];
function getRandomQuestion(qArr) {
let randomNum = Math.floor(Math.random() * 5);
const objectQ = [questions[randomNum].question];
return (objectQ);
}
console.log(getRandomQuestion(qArr));
Are you sure you are returning what’s requested?
Well, I’m returning an object (I know this because I logged the typeof(ObjectQ) to see confirm) with the output: [ 'random question’]. Do the parameters of step 7 imply that I have to ensure that the object does not have any quotation marks even though it classified is an object?
What should typeof(ObjectQ) log to the console? (you might need to check the browser console with F12)
Does the log [ 'random question’] tell you it’s an object?
Some questions about this line.
What do the outer [ ] do?
What is questions ?
What does questions[randomNum] access?
What does questions[randomNum].question access?
It should log the type of ObjectQ, the array containing question objects from the questions array.
The outer brackets ensure the variable is an array object. Without them there, the data type of ObjectQ is a string.
Questions is the original array requested in step 1.
questions[randomNum] accesses a random set of array objects, thus questions[randomNum].question accesses a question from that randomly selected question object within the main array, “questions”.
Right, it should log that objectQ is an array. But aren’t you supposed to return a question object?
- 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.
So you need to return a “question object”, which you are returning in the variable objectQ
So, is it an array or an object?
What’s the difference?
Right, questions is an array of objects*.
No, questions[randomNum] accesses one object in the questions array. aka a “question object”
Yes, it access a question from the question object. But that’s a string.
Don’t you need to return the question object itself? Not a string?
returns a random question object from the array
Thank you for the inquiries! I answered 8, now I’m working on 9-14, but it should be easier.
1 Like
The instructions are very specific about the parameters and return types of the functions.