Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I am failing tests 11, 13, and 14, but my output is correct. I don’t understand what to do next.

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(question) {
let randomNum = Math.floor(Math.random() * 5);
const objectQ = questions[randomNum];
return objectQ;
}
const questionObj = getRandomQuestion();
console.log(questionObj);

function getRandomComputerChoice(choices) {
  let randomNum = Math.floor(Math.random() * 3);
  return choices[randomNum];
}
const choiceObj = getRandomComputerChoice;

function getResults(answer, choices) {
  let randomNum = Math.floor(Math.random() * 5);
  const chosenQ = questions[randomNum].answer;
  const chosenC = questions[randomNum].choices[Math.floor(Math.random() *3)];
  if (chosenQ === questions[randomNum].choices[Math.floor(Math.random() * 3)])
 {
return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${chosenC}`;
  }}
console.log(getResults());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Review each user story and make sure it’s implemented exactly as described. Try not to just focus on the output.

You can start here:

  1. 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.

You have a parameter question but you can see it’s faded out because you never use it in your function. Don’t refer to global variables in the function

I’m trying to create a copy of the original array so that I can extract the values correlating to the 1st position of the clone, but it is returning “undefined”:
let randomNum = Math.floor(Math.random() * 5);

const arrQ = […questions][randomNum][1];

console.log(arrQ);

function getRandomQuestion(arrQ) {

const objectQ = questions[randomNum];

return objectQ;

}

Why?

You were closer on the first try but I’m not sure you fixed the problems that I pointed out.

Can you share the full function? Test all of your variables with console.log to make sure they are what you expect.

To create an array made out of the “question” objects inside the questions array. I will use that new array as a parameter for the getRandomQuestion function. Heres my full code:


let randomNum = Math.floor(Math.random() \* 5);

const arrQ = \[...questions\];

function getRandomQuestion(arrQ) {

const objectQ = questions\[randomNum\];

return objectQ;

}

const questionObj = getRandomQuestion();

console.log(questionObj);



function getRandomComputerChoice(choices) {

  randomNum = Math.floor(Math.random() \* 3);

  return choices\[randomNum\];

}

const choiceObj = getRandomComputerChoice;



function getResults(questionObj, choiceObj) {

  if (choiceObj === questionObj)

 {

return "The computer's choice is correct!";

  } else {

    return \`The computer's choice is wrong. The correct answer is: ${chosenC}\`;

  }

  }



console.log(getResults());

do you think getRandomQuestion is going to return different objects?
or always the same?consider what’s the issue of writing this in the global space let randomNum = Math.floor(Math.random() * 5);

If I put the randomNum declaration inside the function, I still get random results but it doesn’t recognize the question parameter. If I remove the question parameter, step 8 fails- it seems having anything at all in the parameter ensures 8 doesnt fail, but I’m not sure how to pass an array of questions without creating a shallow copy of the questions array.

here you have parameter arrQ but you are not using that inside the function, questions exists outside the function, it’s not a parameter of the function, so I am not sure what you are saying about the question parameter

I’ve reverted my code, this is the getRandomQuestion function:

function getRandomQuestion(question) {

let randomNum = Math.floor(Math.random() * 5);

const objectQ = questions[randomNum];

return objectQ;

}

const questionObj = getRandomQuestion();

it doesn’t look like you are using question parameter at all
I see questions but that’s not the same

don’t forget to pass the argument to the function when you call it

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Now I’m using the question parameter:
function getRandomQuestion(question) {

let randomNum = Math.floor(Math.random() * 5);

const arrQ = […questions][randomNum][question];

return arrQ;

}

const questionObj = getRandomQuestion();

your parameter should be an array of questions

you need to also call the function with an array of questions

you define the function with question parameter (you should give it a better name, it would be an array of questions), do not access any global variable

questions does not exist inside the function, do not use it

then remember you are asked to return a random question object, not the value of a property inside the random question object

I’m trying a bunch of different things, but nothing is working. To retrieve an array of questions, I’m unsure about how to do so if the shallow copy value is undefined. I need the values from the questions array for a specific index randomly, but I don’t understand how to do so without accessing a global variable and not creating a shallow clone.

what do you mean with retrieve? you don’t need to use anything more than the function parameters

share your updated code

By retrieve I am referring to the process of grouping all “question:” objects within the multidimensional array, “questions”.

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”

}];

const arrayQ = questions[Math.floor(Math.random() * 5)];

function getRandomQuestion(arrayQ) {

let randomNum = Math.floor(Math.random() * 5);

const objectQ = questions[randomNum];

return objectQ;

}

const questionObj = getRandomQuestion();

console.log(questionObj);

function getRandomComputerChoice(choices) {

let randomThr = Math.floor(Math.random() * 3);

const choiceC = choices[randomThr];

return choiceC;

}

const choiceObj = getRandomComputerChoice;

function getResults(questionObj, choiceObj) {

if (choiceObj === questionObj){

return ‘The computer’s choice is correct!’;} else {

return `The computer’s choice is wrong. The correct answer is: ${choiceObj}`;

}

}

console.log(getResults());

consider this function in isolation, you have only arrayQ, you don’t have questions do use, Update your function considering this

to use the function you need to call the function (put between the brackets ()) the argument of the function, which will be the array of questions from which you want to get a random question

here you are not calling getRandomComputerChoice, you need to pass to it an array of choices like what’s inside one of the question objects

remember that to getResult you pass a question object, like you get from getRandomQuestion and an answer to check if it’s correct or not, like what you get from getRandomComputerChoice.
Considering that one is an object and one is a string, can you do choiceObj === questionObj?

here you need to pass as arguments a question object and an answer to validate if it’s correct or not

Here’s my updated code. I’m still having difficulty outputting my choice object as an object, not a string. This is confusing because the strict operator in getResults requires them to share type and value:

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”

}];

const arrayQ = questions[Math.floor(Math.random() * 5)];

function getRandomQuestion(arrayQ) {

let randomNum = Math.floor(Math.random() * 5);

const objectQ = arrayQ[randomNum];

return objectQ;

}

const questionObj = getRandomQuestion(questions);

console.log(questionObj);

const sampleC = questions[Math.floor(Math.random() * 5)].choices;

console.log(sampleC);

function getRandomComputerChoice(sampleC) {

return sampleC[Math.floor(Math.random() * 3)];

}

const choiceObj = getRandomComputerChoice(sampleC);

console.log(typeof choiceObj);

function getResults(questionObj, choiceObj) {

if (choiceObj === questionObj){

return `The computer’s choice is correct!`;}

else {

return `The computer’s choice is wrong. The correct answer is: ${choiceObj}`;

}}

console.log(getResults());

why are you generating a random question here too? you have already questionObj that comes from getRandomQuestion

what do you mean choice object?

remember that getResults need to get passed a question object, which is an object, and a choice from the choices array, which is a string
so how do you check if the choice is correct? what’s inside the question object? how is the correct answer indicated?

please call this with a question object and a choice so you can test

Thanks a lot! I passed, finally. That was the longest time I’ve spent on a challenge before, but the forum is very helpful