Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Program performs exactly as described but fails tests 8, 9, 11, 12, and 13. I have tried multiple ways, but this is the only way that works correctly everytime. Whatis Wrong?!?!?!

Your code so far

const questions = [
  { category: "Music", 
  question: "Who was the Beatles first drummer?", 
  choices: ["Ringo Starr", "Pete Best", "Robert Starkey"], 
  answer: "Pete Best" },
  { category: "Music", 
  question: "What was Motley Crue's original name?", 
  choices: ["Dark Blue", "New Years", "Xmas"], 
  answer: "Xmas" },
  { category: "Movies", 
  question: "In what city does Hogwart's reside?", 
  choices: ["London", "Switzerland", "Romania"], 
  answer: "London" },
  { category: "Movies", 
  question: "What sport, is the movie 'Draft Day' about?", 
  choices: ["Basketball", "Baseball", "Football"], answer: "Football" },
  { category: "Music", question: "Who wrote and performed the soundtrack for the movie 'Flash Gordon'?", choices: ["Kiss", "Led Zeppelin", "Queen"], answer: "Queen" },];

let randomQuestion;
let randomAnswer;

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

console.log(getRandomQuestion(questions));

function getRandomComputerChoice(answerArr) {
  randomAnswer = Math.floor(Math.random()*questions[randomQuestion].choices.length);
  //console.log(randomAnswer);
  return answerArr.choices[randomAnswer];
}
console.log(getRandomComputerChoice(questions[randomQuestion]));
 
function getResults(quest, compAnswer) {
  if(quest === compAnswer) {
    console.log("The computer's choice is correct!");
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${questions[randomQuestion].answer}`
  }
}
console.log(getResults(questions[randomQuestion].answer, questions[randomQuestion].choices[randomAnswer]));















Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game
https://www.freecodecamp.org/learn/full-stack-developer/lab-quiz-game/lab-quiz-game

Here are some troubleshooting steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the first failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

I’ve done this 20 times, but here are your answers:

  1. Are there any errors or messages in the console?

  2. What is the requirement of the first failing test?

  3. Check the related User Story and ensure it’s followed precisely.

  4. What line of code implements this?

  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

What does your getRandomQuestion function return? Is it a question object?

return array[randomQuestion].question;

yes, array represents the parameter given to the getRandomQuestion function. I am returning the question object from the questions array at the random array position generated in the getRandomQuestion function.

What is an object in JS?

How would you access one of the question objects in the questions array?

Ive always used dot notation hence the “array[randomQuestion].question

Why are your answers vaguer than the user stories?!?!?!

console.log(typeof array[randomQuestion].question)

You can add this code above the return in your getRandomQuestion() to see what your function is returning. @pkdvalis is trying to guide you to find your error.

Also, I recommend using meaningful variable names and avoiding global variables that are not constants. You’ll thank yourself later as you continue your coding journey.

For example, in your getRandomQuestion() function you set a global variable named randomQuestion. But if you console.log() that variable, you’ll see it’s an integer…and you intend to use it as an index. So, why not name it something like randomIndex instead?

What is an object in JS?

An object is created with curly brackets and has key:value pairs. This is an object:

{ category: "Music", 
  question: "Who was the Beatles first drummer?", 
  choices: ["Ringo Starr", "Pete Best", "Robert Starkey"], 
  answer: "Pete Best" }

You have an array of objects. You’re accessing an object from the array with bracket notation and then accessing a property of that object with dot notation.

You need to return a question object, not a question string.

I’m asking specific questions for a reason. If you investigate what I’m asking about you will find the problem. If I’m repeating a question it should be clear that you’re not giving a correct answer, right?

Tell us what’s happening:

Now I am stuck on the last three 11, 12, 13 I am passing the question object as the first parameter and a variable (computerAnswer) which contains the result of the getRandomComputerChoice function. and it fails all 3 checks. I have tried reworking it and pas, the question object as the first parameter, and the getRandomComputerChoice with basically the same results, code works but fails tests. Any guidance is apreciated.

Your code so far

const questions = [
  { category: "Music", 
  question: "Who was the Beatles first drummer?", 
  choices: ["Ringo Starr", "Pete Best", "Robert Starkey"], 
  answer: "Pete Best" },
  { category: "Music", 
  question: "What was Motley Crue's original name?", 
  choices: ["Dark Blue", "New Years", "Xmas"], 
  answer: "Xmas" },
  { category: "Movies", 
  question: "In what city does Hogwart's reside?", 
  choices: ["London", "Switzerland", "Romania"], 
  answer: "London" },
  { category: "Movies", 
  question: "What sport, is the movie 'Draft Day' about?", 
  choices: ["Basketball", "Baseball", "Football"], 
  answer: "Football" },
  { category: "Music", question: "Who wrote and performed the soundtrack for the movie 'Flash Gordon'?", choices: ["Kiss", "Led Zeppelin", "Queen"], 
  answer: "Queen" },];

let randomQuestion;
let randomAnswer;
let computerAnswer;

function getRandomQuestion(array) {
  randomQuestion = Math.floor(Math.random()*array.length);
  console.log(randomQuestion);
  return array[randomQuestion];
  }
console.log(getRandomQuestion(questions).question);
 
function getRandomComputerChoice(answerArr) {
  randomAnswer = Math.floor(Math.random()*questions[randomQuestion].choices.length);
  computerAnswer = answerArr[randomAnswer];
  return answerArr[randomAnswer];
}
function getResults(quest, compAnswer) {
  if(quest[randomQuestion].answer === compAnswer) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${questions[randomQuestion].answer}`
  }
}
console.log(getRandomComputerChoice(questions[randomQuestion].choices));
console.log(getResults(questions, computerAnswer));






















Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game
https://www.freecodecamp.org/learn/full-stack-developer/lab-quiz-game/lab-quiz-game

why are you not using answerArr to select the random answer?

also double check what the arguments for this function are

I changed the gatRandomComputerChoice to use answerArr for the function (which had nothing to do with the issue I am having. That portion of the code was working and passed the tests. What still is working but not passing the tests is the getResults function. In the instructions it says “Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.” I am doing this, and have tried it multiple ways, In my getRandomComputerChoice, I assigned the computerAnswer and tried passing that to the getresults as the second parameter and it works but fails the test. Ive tried calling the getrandomComputerChoice in the getResults function call, but that generates a new answer Im unclear what “arguements” you are referring to, unless you are talking about the “parameters”

a function is defined with parameters, then it is called with arguments and the value of the arguments is given to the parameters

are you treating the first parameter as the question object?

The first parameter you are passing to getResults() is questions. Is that an array or is that a question object?

Thank yoou, that was very helpful. I had originally had it this way but had something else wrong. I am still failing the last test. But the program works as it should. I have tried to return a concatenated string and I used back ticks and ${} but both ways it is failing. I really wish the user stories were more specific. OR the test responses, because “getResults should return The computer’s choice is wrong. The correct answer is: , where is the value of the correct answer to the chosen question.” Is NOT helpful because that is EXACTLY what me getResults function returns. WHAT is it not liking? because it works correctly EVERYTIME

There are probably a variety of ways to solve any problem, but the instructions are asking for this to be implemented in a very specific way. You need to implement precisely as instructed or it will not pass the tests, even if the output seems to work.

I would suggest going through each User Story and making sure the input parameters and output data types are handled as asked. It’s important to pay attention to the user stories as that is the actual instructions. The tests are just tests and will fail for a variety of reasons that might not be clear from the test feedback.

You can pay special attention to the input parameters and return values requested, and if they are to be objects, arrays or strings etc.

THAT is the problem there is very little specificity. The instructions (user story) are:

  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. The function should return The computer's choice is correct! if the answer is correct. Otherwise, it returns The computer's choice is wrong. The correct answer is: <correct-answer>, where <correct-answer> is the value of the correct answer to the chosen question. The only specific instruction is The first parameter is the question object. Which I am calling with the “quest” parameter. The only other instruction is the second parameter should be the “Computer answer”, it doesnt specify what form that be in IE: a variable, or calling the getRandomComputerChoice. Which does not always work, at least it changes from what I get in the console log call of that function. The way I have it now works 100% of the time. If you are looking for a specific solution the instructions should be better written to reflect that

If you still need assistance you’ll need to share the latest version of your code.

I dont see a way to reshare, and someone gave me shit for starting a new topic for this same quiz, so here it is:

const questions = [

{ category: “Music”,

question: “Who was the Beatles first drummer?”,

choices: [“Ringo Starr”, “Pete Best”, “Robert Starkey”],

answer: “Pete Best” },

{ category: “Music”,

question: “What was Motley Crue’s original name?”,

choices: [“Dark Blue”, “New Years”, “Xmas”],

answer: “Xmas” },

{ category: “Movies”,

question: “In what city does Hogwart’s reside?”,

choices: [“London”, “Switzerland”, “Romania”],

answer: “London” },

{ category: “Movies”,

question: “What sport, is the movie ‘Draft Day’ about?”,

choices: [“Basketball”, “Baseball”, “Football”],

answer: “Football” },

{ category: “Music”, question: “Who wrote and performed the soundtrack for the movie ‘Flash Gordon’?”, choices: [“Kiss”, “Led Zeppelin”, “Queen”],

answer: “Queen” },];

let randomQuestion;

let randomAnswer;

let computerAnswer;

function getRandomQuestion(array) {

randomQuestion = Math.floor(Math.random()*array.length);

console.log(randomQuestion);

return array[randomQuestion];

}

console.log(getRandomQuestion(questions).question);

function getRandomComputerChoice(answerArr) {

randomAnswer = Math.floor(Math.random()*answerArr.length);

computerAnswer = answerArr[randomAnswer];

return answerArr[randomAnswer];

}

function getResults(quest, compAnswer) {

if(quest.answer === compAnswer) {

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

} else {

return \`The computer's choice is wrong. The correct answer is: ${questions\[randomQuestion\].answer}\`;

}

}

console.log(getRandomComputerChoice(questions[randomQuestion].choices));

console.log(getResults(questions[randomQuestion], computerAnswer));

Please format your code when you want to share it, otherwise other people are not able to read it properly

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