Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Tests 8, 9, 11, 13, and 14 are returning false. I need a little help. The code works perfectly fine but the tests just aren’t getting cleared.

Your code so far

const questions = [
  {
    category: "astronomy",
    question: "What does astronomy study?",
    choices: ["living organisms", "weather", "space objects"],
    answer: "space objects"
  },
  {
    category: "country area",
    question: "Which country is the largest by area?",
    choices: ["Russia", "USA", "Canada"],
    answer: "Russia"
  },
  {
    category: "leap year",
    question: "How many days in a leap year?",
    choices: ["365", "366", "364"],
    answer: "366"
  },
  {
    category: "chemical symbol",
    question: "Which chemical element has the symbol O?",
    choices: ["Gold", "Ozone", "Oxygen"],
    answer: "Oxygen"
  },
  {
    category: "largest organ",
    question: "What is the largest organ in the human body?",
    choices: ["skin", "heart", "liver"],
    answer: "skin"
  },
];

let random = Math.floor(Math.random() * questions.length);
let randoans = questions[random].answer;
let answer = randoans;

function getRandomQuestion(array) {
  return array[random].question;
};

let randoquest = getRandomQuestion(questions);

function getRandomComputerChoice(array) {
  let randoc = Math.floor(Math.random() * 3);
  return array[random].choices[randoc];
};

let randocho = getRandomComputerChoice(questions);

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

let result = getResults(randoquest, randocho);

console.log(randoquest);
console.log(randocho);
console.log(result);

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 Edg/144.0.0.0

Challenge Information:

Build a Quiz Game - Build a Quiz Game

is this able to give you a different random question every time it’s run? or it will be always the same question because random has always the same value?

are you sure what array is? double check what the user stories say about getRandomComputerChoice

if the parameters are question and choice where does answer come from?

It gives me a random question each time, I put random outside the functions so that random will be one number for all functions meaning it will pull questions, choice and answer from the same index in the questions array

After the function I created a variable that inputs questions as the argument for array

Answer was already defined using random. I tried defining it inside the function using question and the questions array but the tests still returned false

You get a random result every time you run the entire program.

If you call the function twice, as the tests might, you will get the same result, because a new random number has not been generated.

Try not to have global variables. Only when absolutely necessary. Use the parameters of the functions.

I am using a global variable so that random for the question, choices and answer will return the same index. If I declare it inside each function block separately, random will return different numbers and with it, unassociated questions, choices and answers

and why do you need that? All three functions have different parameters and return different things, there is no reason they need to have the same random number

what you say is not even true because you are generating a different random number inside getRandomComputerQuestion

I’m generating two random numbers as “array[random].choices[randoc]” so that random gets a random questions index from which we choose a choices property, but choices actually is another array so randoc is for picking a random option from the array

and you need to pick a random number for the question inside getRandomQuestion otherwise you make your function not reusable

like, consider if I want three random questions in a row

const question1 = getRandomQuestion(questions);
const question2 = getRandomQuestion(questions);
const question3 = getRandomQuestion(questions);

I will get the same question 3 times, that’s not what I want

I did try putting the random variable inside the function blocks but it still returned false. Only this time, it was completely random questions, choices and answers not even related to one another

post your updated code

const questions = [
  {
    category: "astronomy",
    question: "What does astronomy study?",
    choices: ["living organisms", "weather", "space objects"],
    answer: "space objects"
  },
  {
    category: "country area",
    question: "Which country is the largest by area?",
    choices: ["Russia", "USA", "Canada"],
    answer: "Russia"
  },
  {
    category: "leap year",
    question: "How many days in a leap year?",
    choices: ["365", "366", "364"],
    answer: "366"
  },
  {
    category: "chemical symbol",
    question: "Which chemical element has the symbol O?",
    choices: ["Gold", "Ozone", "Oxygen"],
    answer: "Oxygen"
  },
  {
    category: "largest organ",
    question: "What is the largest organ in the human body?",
    choices: ["skin", "heart", "liver"],
    answer: "skin"
  },
];

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

function getRandomComputerChoice(array) {
  let random = Math.floor(Math.random() * questions.length);
  let randoc = Math.floor(Math.random() * 3);
  return array[random].choices[randoc];
};

function getResults(question, choice) {
  let random = Math.floor(Math.random() * questions.length);
  let answer = question[random].answer;
  if (choice === answer) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${answer}`;
  };
};

let randoquest = getRandomQuestion(questions);
let randocho = getRandomComputerChoice(questions);
let randoans = getResults(questions, randocho);

console.log(randoquest);
console.log(randocho);
console.log(randoans);
console.log(randoquest);
console.log(randocho);
console.log(randoans);

Tests 8, 9, 11, 12, 13 and 14 return false

you have the array parameter, you should use it, not questions.length

are you sure that this function returns an object as requested?

please review what is array in this function, and once you know you need to use only that, not questions
this is the related user story:

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

you have question and choice in this function, what is answer? where does that come from?

The functions act together. You’ll make one main function call.

  1. You should have a function named getResults that takes the question object as the first parameter and the computer’s choice as the second parameter.

This is where the question and computer’s choice need to be synced, in that the computer’s choice needs to be from the same question as the random question passed to the function.

To test your program I would suggest one global variable, a random question, which you can pass as the question to get results, and then use to pass to the getRandomComputerChoice function to get a random computer choice as the second argument.

let q = getRandomQuestion(questions)
console.log(getResults(q, getRandomComputerChoice(q.choices)))

This way both are derived from the same random question.

Other than this, each function should operate totally independently. getRandomQuestion(questions) should return a random question every time it’s called.

1 Like