Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Hey,

My code is passing sometimes and failing the other. Obviously to do with the Math. Random I’ve possibly messed up. Can someone help me decipher what’s causing this?

It might also be because its very late but I am not seeing where the issue lies. Assuming its within computer choice not having the correct search parameter or return.

Your code so far

const questions = [];

const q1 = {
  category: "Colour",
  question: "Whats colour's best?",
  choices: ["Cyan", "Teal", "Pink"],
  answer: "Teal"
};


const q2 = {
  category: "Hate",
  question: "Who do you hate more?",
  choices: ["Boss", "Kids", "Java"],
  answer: "Boss"
};

const q3 = {
  category: "Home",
  question: "Will you buy a house?",
  choices: ["Maybe", "Yes", "No"],
  answer: "Yes"
};

const q4 = {
  category: "X-Men",
  question: "Who is the best X-Men?",
  choices: ["Iceman", "Magic", "Sunspot"],
  answer: "Iceman"
};

const q5 = {
  category: "Games",
  question: "What one do you like more?",
  choices: ["Bloodborne", "FF14", "Marvel"],
  answer: "Bloodborne"
};

questions.push(q1, q2, q3, q4, q5);

function getRandomQuestion(questions) {
  let randomQ = Math.floor(Math.random() * 5);
  return questions[randomQ];
};

function getRandomComputerChoice (choices) {
  let randomC = Math.floor(Math.random() * 5);
  return choices[randomC];
};

function getResults(question, getRandomComputerChoice) {
  if (getRandomComputerChoice == question.answer) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${question.answer}`;
  };
};

console.log(getResults(q4));

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

How can you investigate this?

Use console.log to see what’s happening in that function.

  console.log(randomC)
  console.log(choices[randomC])

what happens if questions have 10 questions? would this work?

Ive changed it to length of questions length and questions choices length respectively.

Working on why its saying randomC not defined

In doing so ive found that randomC and choices are not defined.

I realise I am not actually searching the length of the chosen question but I am not sure how to get that. I was going to make a result within the get random question but that would be local and not the actual result. I could use the actual call for the function itself or would that not bring a seperate random result. I am starting to get more lost as I think about it.

What line of code generates the error?

You’ve addressed the correct problem, but make sure you don’t have an “off by one” error.

Please share your updated code

questions.push(q1, q2, q3, q4, q5);

function getRandomQuestion(questions) {
  let randomQ = Math.floor(Math.random() * questions.length);
  return questions[randomQ];
};

function getRandomComputerChoice (choices) {
  let randomC = Math.floor(Math.random() * getRandomQuestion.choice.length);
  return choices[randomC];
};

function getResults(question, getRandomComputerChoice) {
  if (getRandomComputerChoice == question.answer) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${question.answer}`;
  };
};

console.log(getResults(q4));

This is the updated code. I am not sure I am successfully grabbing the length of the arrays needed. I thought grabbing the length of the choices within the randomly selected question would give me the correct computer choice. But I am getting
“TypeError: Cannot read properties of undefined (reading ‘length’)”

Really struggling with this one but I am more struggling with understanding arrays within object handling and if I am doing it correctly.

Could be objects in general I am really struggling with.

What is the function parameter here?

Do question objects in the questions array have a choice property?

What parameters does getResults take?

Okay so I think this is it.

I am now paranoid that its only passing sometimes and that I’ve not actually done it.
I added 2 consts specifically for my getResults console log. I feel like there is a better method than creating 2 consts to get the necessary parameters but cannot work out what that is.

const randomQuestion = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(randomQuestion.choices);
console.log(getResults(randomQuestion, computerChoice));

Secondly I fixed the length and function parameter. For the random comp choice… I think.

function getRandomComputerChoice(choices) {
  let randomC = Math.floor(Math.random() * choices.length);
  return choices[randomC];
};

Finally I redid the getResults so I could do the const method for the console log

function getResults(question, compChoice) {
  if (compChoice == question.answer) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${question.answer}`;
  };
};

If this is all fine and together with the rest of the code is working properly and not just sometimes. Then I have several questons.

When getting a length for a property inside an object you have 2 methods.

dot . and bracket [ ]

when using them for getting the length I used it on the getRandomQuestion. Trying to use that .choices.length did not work. Would it not be randomQuesion result q2.choices.length.

Or does the code read this as function, function does not have choices, choices undefined?

Second question. In the functions I am passing questions and choices as parameters. Are these specifically choices that is within the object and questions array or is it still a placeholder for what gets used in the call()

I thank you all for the time taken to help me with this. I have been getting so frustrated with this one I have rewrote it multiple times because I cannot understand the Type error of length undefined when I was trying to define said length.

Yes, the parameter is entirely dependant on what you pass in the function call. The parameter is just a variable that gets populated by whatever you pass in the call.

This didn’t work because getRandomQuestion is not a variable defined in the function. You should not really access global variables within the function either. You should only work with data that is pass in by parameters.

Additionally that property was called choices, not choice

You also should not name a variable with the same name as a function it’s just confusing and there’s no reason to do it.

Thank you for answering my questions. Going through javascript has defiantly been harder than getting the HTML and CSS cert.

The walls of theory then with a bit of lab work after learning so much has been the hardest part for me to take in the information. So having you take the time to read and reply has been incredibly helpful thank you.

Same with everyone else helping along the way.
Thank you all.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.