Build a Quiz Game: getResults

Code is working properly but is not passing 11, 12, 13, 14. I’ve been trying to change some things to get it to pass but it has not worked.

const questions = [
  {category:"Anatomy",
  question:"Besides its use as a sensory apparatus, what is another purpose of the hair on a tarantula's body?",
  choices: ['Territory', 'Defense', 'Warmth'],
  answer: 'Defense'}
  ,
  {category:"Types",
  question:"There are basically two types of these spiders, the 'terrestrial' or ground-dwelling tarantulas and ...?",
  choices: ['Aboreal', 'Old World', 'New World'],
  answer: 'Aboreal'}
,
  {category:"Climate",
  question:"Tarantulas are capable of surviving in all but ...?",
  choices: [ 'Desert', 'Arctic', 'Tropical'],
  answer: 'Arctic'}
  ,
  {category:"Lifespan",
  question:"Female tarantulas are blessed with a lifespan that is longer than most arachnoid species. About how long can one live under ideal conditions?",
  choices: ['10-15 years', 'up to 20 years', '20+ years'],
  answer: '20+ years'}
  ,
  {category:"Hunting",
  question:"How do tarantulas find their food?",
  choices: ['Sound', 'Smell', 'Vibrations'],
  answer: 'Vibrations'}
]

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

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

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

let selectedQuestObject = getRandomQuestion(questions);
let availableChoices = selectedQuestObject.choices;
let key = selectedQuestObject.answer;
let computerChoice = getRandomComputerChoice(availableChoices);
let grade = getResults(key, computerChoice);
let question = selectedQuestObject.question;

console.log("Question:", question);
console.log("Computer chose:", computerChoice);    
console.log("Grade:", grade); 
  1. I thought I was passing getResult parameters correctly with questionObject first and computerChoice second. with getResults(key, computerChoice) & let key = selectedQuestObject.answer
  2. It is comparing and returning when it is incorrect, correctly.
  3. It is comparing and returning when it is correct, correctly.
  4. (computerChoice === key) used for comparison.
  • 11. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.

  • Failed:12. If the computer choice matches the answer, getResults should return The computer's choice is correct!

  • Failed:13. If the computer choice doesn’t match the answer, getResults should return 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.

  • Failed:14. Your getResults function should use exact equality comparison, not substring matching.

Please share a link to the lesson you are working on.

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

1 Like
  1. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.

function getResults(key, computerChoice) {

let key = selectedQuestObject.answer;

It seems to are passing an answer to getResults and not the question object

Hi @catheriine1996

8. 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 posted code for the object, you also need to add a function.

Edit: it looks like you updated your post with the new functions.

11. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.

As @pkdvalis mentioned, you need to need to pass an object.

Happy coding

Tackle one Test at a time, and make sure to carefully review the User Story and make sure you are following it.

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.

Tell us what’s happening:

Failing 11, 12, 13, 14
11. I thought I was passing getResult parameters correctly with questionObject first and computerChoice second. with getResults(key, computerChoice) & let key = selectedQuestObject.answer
12.It is comparing and returning when it is incorrect, correctly.
13.It is comparing and returning when it is correct, correctly.
14.(computerChoice === key) used for comparison.

Your code so far

const questions = [
  {category:"Anatomy",
  question:"Besides its use as a sensory apparatus, what is another purpose of the hair on a tarantula's body?",
  choices: ['Territory', 'Defense', 'Warmth'],
  answer: 'Defense'}
  ,
  {category:"Types",
  question:"There are basically two types of these spiders, the 'terrestrial' or ground-dwelling tarantulas and ...?",
  choices: ['Aboreal', 'Old World', 'New World'],
  answer: 'Aboreal'}
,
  {category:"Climate",
  question:"Tarantulas are capable of surviving in all but ...?",
  choices: [ 'Desert', 'Arctic', 'Tropical'],
  answer: 'Arctic'}
  ,
  {category:"Lifespan",
  question:"Female tarantulas are blessed with a lifespan that is longer than most arachnoid species. About how long can one live under ideal conditions?",
  choices: ['10-15 years', 'up to 20 years', '20+ years'],
  answer: '20+ years'}
  ,
  {category:"Hunting",
  question:"How do tarantulas find their food?",
  choices: ['Sound', 'Smell', 'Vibrations'],
  answer: 'Vibrations'}
]

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

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

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

let selectedQuestObject = getRandomQuestion(questions);
let availableChoices = selectedQuestObject.choices;
let key = selectedQuestObject.answer;
let computerChoice = getRandomComputerChoice(availableChoices);
let grade = getResults(key, computerChoice);
let question = selectedQuestObject.question;

console.log("Question:", question);
console.log("Computer chose:", computerChoice);    
console.log("Grade:", grade);   



Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Hi @catheriine1996

I went ahead and combined your posts for you. In the future, just reply to the original thread to add further updates.

Thanks.

Thank you! Sorry about double posting.

I have managed to figure it out. The fix to 11 also forced a fix for the rest to make it work properly. Thank you so much.

2 Likes