Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I genuinely do not understand how getResults is supposed to work and how is supposed to take two parameters that depend on previous functions. Please advice I’m lost

Your code so far

const questions = [
  {
    category: "Geography",
    question: "What is the capital of Japan?",
    choices: ["Tokyo", "Berlin", "Panama"],
    answer: "Tokyo"
  },
  {
    category: "US History",
    question: "Who was the first U.S. president?",
    choices: ["Donald Trump", "George Washington", "Paul George"],
    answer: "George Washington"
  },
  {
    category: "Astronomy",
    question: "What planet is known as the Red Planet?",
    choices: ["Earth", "Mercury", "Mars"],
    answer: "Mars"
  },
  {
    category: "Sports",
    question: "How many players are on a soccer field per side?",
    choices: ["11", "12", "10"],
    answer: "11"
  },
  {
    category: "Biology",
    question: "What gas do humans breathe in to survive?",
    choices: ["Helium", "Oxygen", "Argon"],
    answer: "Oxygen"
  },
];

function getRandomQuestion (arr) {
  let random = Math.floor(Math.random() * 5);
  return arr[random]
}

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

function getResults (obj, choice) {
  obj = getRandomQuestion(questions);
  choice = getRandomComputerChoice(questions[obj].choices)
  if (choice === obj.answer) {
    return `The computer's choice is correct!`
  }
  return `The computer's choice is wrong. The correct answer is: ${obj.answer}`
}

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-quiz-game/66f17db06803d11a1bd19a20.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @JeremiG936,

You can create these variables in the global space, then pass them into your getResults() function call.

Also, when you create a function definition, there should be no space between the function name and the opening parenthesis.

And please don’t hardcode values like this.

Happy coding

getResults() seems to be working now but its still say I’m missing points 11, 13 and 14

const questions = [

{

category: "Geography",

question: "What is the capital of Japan?",

choices: \["Tokyo", "Berlin", "Panama"\],

answer: "Tokyo"

},

{

category: "US History",

question: "Who was the first U.S. president?",

choices: \["Donald Trump", "George Washington", "Paul George"\],

answer: "George Washington"

},

{

category: "Astronomy",

question: "What planet is known as the Red Planet?",

choices: \["Earth", "Mercury", "Mars"\],

answer: "Mars"

},

{

category: "Sports",

question: "How many players are on a soccer field per side?",

choices: \["11", "12", "10"\],

answer: "11"

},

{

category: "Biology",

question: "What gas do humans breathe in to survive?",

choices: \["Helium", "Oxygen", "Argon"\],

answer: "Oxygen"

},

];

function getRandomQuestion(arr) {

let random = Math.floor(Math.random() * arr.length);

return arr[random]

}

function getRandomComputerChoice(arr) {

let random = Math.floor(Math.random() \* arr.length);

return arr\[random\]

}

let myObj = getRandomQuestion(questions);

let theAnswer = myObj.answer;

let theChoice = getRandomComputerChoice(myObj.choices);

function getResults() {

if (theChoice === theAnswer) {

return \`The computer's choice is correct!\`

}

return `The computer’s choice is wrong. The correct answer is ${theAnswer}`

}

console.log(getResults())

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

Please review User Story #9 again:

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.