Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

My code seems to be functioning properly when I log to the console. However, if I run the test multiple times, it gives me different failures each time. I am not sure what the precise problem is, but 13 seems to be failing most frequently. 1 and 2 has also failed a few times, which was strange.

Your code so far


const questions = [
  {
  category: 'Mathematics',
  question: 'What is 2 x 7?',
  choices:['a: 21', 'b: 42', 'c:14'],
  answer: 'c:14'
},
{
  category:'English',
  question:'What is the antonym of hot?',
  choices: ['warm', 'spicy', 'cold'],
  answer: 'cold'
},
{
  category: 'Science',
  question:'How many bones are in the adult human body?',
  choices: ['270', '206', '109'],
  answer: '206'
},
{
  category: 'Programming',
  question:'Which one of the following is a programming language?',
  choices:['Javascript', 'Pythos', 'Mocha'],
  answer: 'Javascript'
},
{
  category: 'History',
  question:'In which year did World War 2 end?',
  choices: ['1900', '1976', '1945'],
  answer: '1945'
}];

let index = Math.round(Math.random() * questions.length);
let choiceIndex = Math.round(Math.random());


function getRandomQuestion(array){ 
  let randomQuestionObject = array[index];
  return randomQuestionObject;
}

let randomObject = getRandomQuestion(questions);
let choices = randomObject.choices;

function getRandomComputerChoice(choices){
 let computerChoice = choices[choiceIndex];
 return computerChoice;
}

let computerChoice = getRandomComputerChoice(choices);

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


getResults(randomObject, computerChoice); 

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

What happens when you create an index using Math.random() in the global space compared to creating it within the function? Console.log() your values so you can see what your code is producing.

Also, does this syntax look correct to you. How does it compare to the syntax you used for index? Try logging the result to see what’s happening here. And should you be using Math.round() or something else?

Carefully check the message you are returning from getResults() when the computer choice is wrong. Your message does not match the instructions exactly.

[quote=“fcc4b6d10c4-b540-4e2, post:2, topic:751493, full:true”]
What happens when you create an index using Math.random() in the global space compared to creating it within the function?
[/quot

Thank you for the feedback. I think that would cause my index to be fixed to the variable value. I’ve now placed the index inside the functions instead of outside, however the code is still not passing..

if you still need help please make possible for people to help you

I edited my first post to include more to look into.

I’m not sure how to share my updated code on this thread. What is the preferred method?

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 (').

Thank you both for the guidance. I am going to spend another 30 minutes trying to fix this before I share my updated code.

Okay, it passed now. The three key fixes I made were 1) moving the random index selection inside the functions 2) correcting the return statement of my getResults function, and 3) changing Math.round(Math.random()…) to Math.floor(Math.random…).
However, I do not understand why Math.floor worked and Math.random did not. They both have the rounding function? Albeit, Math.round moves up and not only down. Here is my updated code:

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

let randomObject = getRandomQuestion(questions);
let choices = randomObject.choices;

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

let computerChoice = getRandomComputerChoice(choices);

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

Using the index for choices as an example, you would want to return values 0, 1, or 2. If you use Math.round(), it’s possible to get 3, which you don’t want. But because Math.floor() always rounds down, that won’t happen. But don’t take my word for it. Try it! Console.log() what it produces over and over and eventually you will see that Math.round(Math.random() * 3) produces 3.

1 Like

We have blurred this solution (with [spoiler][/spoiler] tags) so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

1 Like

Got it, will refrain from posting any solutions.

1 Like