Build a Quiz Game - Build a Quiz Game

I am having issues with the Build a Quiz Game Lab.

For some reason, it is not passing the last 2 test cases for ensuring the two values being compared are using a strict equality comparison and the function returns the correct string when the computer is wrong. I added debugging messages in the program to ensure the right values are being stored throughout the program and don’t know why it’s failing.

I also noticed when I tried wrapping the correct answer in “< >” it wouldn’t output to the console which you can see if you log the code below into a terminal.

MY CODE:

const obj1 = {
  category: "Coding Questions | obj1",
  question: "Do you like JavaScript?",
  choices: ["Yes", "No", "Absolutely"],
  answer: "Absolutely"
};
const obj2 = {
  category: "Coding Questions | obj2",
  question: "Do you like JavaScript?",
  choices: ["Yes", "No", "Absolutely"],
  answer: "Absolutely"
};
const obj3 = {
  category: "Coding Questions | obj3",
  question: "Do you like JavaScript?",
  choices: ["Yes", "No", "Absolutely"],
  answer: "Absolutely"
};
const obj4 = {
  category: "Coding Questions | obj4",
  question: "Do you like JavaScript?",
  choices: ["Yes", "No", "Absolutely"],
  answer: "Absolutely"
};
const obj5 = {
  category: "Coding Questions | obj5",
  question: "Do you like JavaScript?",
  choices: ["Yes", "No", "Absolutely"],
  answer: "Absolutely"
};

const questions = [obj1, obj2, obj3, obj4, obj5];

function getRandomQuestion(questions) {
  const min = 0;
  const max = questions.length;

  let objVal = Math.floor(Math.random() * (max - min) + min);

  return questions[objVal];
}

const objChoice = getRandomQuestion(questions);

function getRandomComputerChoice(objChoicesArr) {
  const min = 0;
  const max = 3;

  let choicesVal = Math.floor(Math.random() * (max - min) + min);

  return objChoicesArr[choicesVal];
} 

const objChoicesArr = objChoice.choices;
const objComputerChoice = getRandomComputerChoice(objChoicesArr);

function getResults(objChoice, objComputerChoice) {
  if (objComputerChoice === objChoicesArr[2]) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${objChoice.choices[2]}`
  }
}

// Debugging
console.log(objChoice);
console.log(objComputerChoice);
console.log(getResults(objChoice, objComputerChoice));

console.log("###### BREAK | BELOW IS DEBUGGING MESSAGE ######");
console.log("The computer's choice is wrong. The correct answer is: " + objChoice.choices[2] + ">");
console.log("The computer's choice is wrong. The correct answer is:" + "<" + objChoice.choices[2]);
console.log("The computer's choice is wrong. The correct answer is:" + "<" + objChoice.choices[2] + ">");

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Failed Test Cases:

13. If the computer choice doesn’t match the answer, 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.

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

function getResults(objChoice, objComputerChoice) {
  if (objComputerChoice === objChoicesArr[2]) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${objChoice.choices[2]}`
  }
}

Could you explain what each parameter of the getResult function are used for?

1 Like

The objChoice parameter represents one of the 5 objects that is an element of the questions array. The objComputerChoice is a randomly generated value that selects an element’s index. This grabs an element from the choices array from one of the objects (the choices property). The getResults() function then compares if the computer’s answer (the element that was chosen from the choices array) matches the correct answer.

If these two parameters are equal (the computer correctly guessed “Absolutely” to be the correct element within the array of the choices property) then the return statement within the if-branch executes, otherwise the else-branch executes.

so where does objChoicesArr come from?

and is it always the third element that is the correct anwer?
don’t you have a property specifically for the correct answer?
note that the tests may not use the questions you have written

1 Like

Yeah, I realize you’re absolutely right. I revised the code, so it compares against the value that is held by the answer property within the randomly chosen object. This is to avoid any test errors where the answer may not be located at that EXACT location in the choices array.

The objChoicesArr variable stores the array of elements that comes from the choices property of the random object. I use this as an argument to pass to the getRandomComputerChoice() function, so that it may randomly select a string from that array. I use this selection the computer made to compare against the answer that is defined as a value for the answer property for the random object.

I made these changes, continued to debug, and managed to pass all the test cases with these changes:

code removed

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

Ah, I understand. I’ll take note of that going forward, thank you!

1 Like