Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I’m not sure where I’m going wrong the console is logging the details that the challenge asks for.

I’m not able to pass 8,9,11,12,13 and 14

Your code so far

const questions = [
  { category: "favCol",
    question: "What's my favourite colour?",
    choices: ["red","blue","pink"],
    answer: "pink",
  },
  {
    category: "favCar",
    question: "What's my favourite car?",
    choices: ["mazda","ferrari","honda"],
    answer: "ferrari",
  },
  {
    category: "favInst",
    question: "What's my favourite Instrument?",
    choices: ["bass","guitar","piano"],
    answer: "bass",
  },
  {
    category: "favAnimal",
    question: "what's my favourite Animal?",
    choices: ["cat","dog","mouse"],
    answer: "mouse",
  },
  {
    category: "favSub",
    question: "What's my favourite suburb?",
    choices: ["brunswick","northcote","caulfield"],
    answer: "brunswick",
  },
];


let randomQ = Math.floor(Math.random() * (4 - 0) + 0);
function getRandomQuestion() {
  let ranQ = questions[randomQ].question
  return ranQ
};

console.log(getRandomQuestion());

let randomA = Math.floor(Math.random() * (2))

function getRandomComputerChoice() { 
  let ranA = questions[randomQ].choices[randomA]
  return ranA
};

console.log(getRandomComputerChoice());

function getResults() {
  let result;
  if (questions[randomQ].choices[randomA] === questions[randomQ].answer) {
    result = "The computer's choice is correct"
  } else if (questions[randomQ].choices[randomA] !== questions[randomQ].answer) {
    result = "The computer's choice is wrong. The correct answer is : " + questions[randomQ].answer
  }
  return result
}

console.log(getResults());


Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Take another look at the requirements for getRandomComputerChoice and getResults, they both should accept single argument.

I’m a little confused what you mean.

Are you saying that there should be an input for the function? e.g. rather than just running getResults(), I should be running something like getResults(obj)?

Yes, that’s exactly it.

thank you! I’ve gotten most of it figured out but I’m stuck on “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”

those objects are defined in the functions which means I can’t call on them when running getResults(a, b), I can’t figure out how these would become parameters for the getResults function

What if you for a bit try to isolate writing the getResult function, by assuming that whomever uses it, is responsible for providing the correct arguments for it? Leave how that will get here away for a bit. For getResult, first parameter will be this, the second this, nothing more. Now, how getResult would use them to do what the function needs to do?

I’ve made that, here’s the function

// a = array with question, b = computer choice
function getResults(a, b) {
  let result;
  if (questions[a].choices[b] === questions[a].answer) {
    result = "The computer's choice is correct"
  } else if (questions[a].choices[b] !== questions[a].answer) {
    result = "The computer's choice is wrong. The correct answer is : " + questions[a].answer
  }
  return result
}

in this a would = random number for the question and b is the comps choice.

From what I can tell, what’s failing is that I’m not able to use the question object and the computers choice because they are defined inside functions and I can’t reference them in a seperate function

The first parameter should be object with the individual question, not the array of questions. The second one - a single choice.

For example, first parameter:

  {
    category: "favCar",
    question: "What's my favourite car?",
    choices: ["mazda","ferrari","honda"],
    answer: "ferrari",
  }

Second parameter:

"honda"

thank you for spelling it out so plainly was really scratching my head and finally solved it!

1 Like