Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I don’t see what I’m doing wrong. I’m stuck on the get results function .

Your code so far

let questions = [
  
  {
    category: "name",
    question: "your name?",
    choices: ["dave","josh","tim"],
    answer: "tim",
     },
 {
    category: "age",
    question: "your age?",
    choices: ["22","33","44"],
    answer: "44",
  },
  {
    category: "birth date",
    question: "your birth date?",
    choices: ["02/06/2004","11/14/1961","01/23/1968"],
    answer: "11/14/1961",
  },
  {
     category: "address",
     question: "your address?",
     choices: ["mockingbird lane","north portland avenue", "batchelder street"],
     answer: "mockingbird lane",
  },
  {
    category: "contact method",
    question: "contact method?",
    choices: ["Text","smoke signals","Email"],
    answer: "smoke signals",
  }
  
  ];/*
  let rndmNum = Math.round(Math.random() * questions.length) + 5;
  for(let i=0;i < questions.length;i++){
  let rndmQ = questions[i].question;
console.log(rndmQ)
}
for(let i = 0;i < questions.length;i++){
  let answers = questions[i].answer
   console.log(answers)

}
*/
let rndmNum = Math.floor(Math.random() * questions.length)
function getRandomQuestion(questions){
  return questions[rndmNum]
}
//console.log(questions.length)
let rndmChoice = Math.
round(Math.random() * questions[rndmNum])
//console.log(questions)
function getRandomComputerChoice(choices){
  
return choices[rndmNum]
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15

Challenge Information:

Build a Quiz Game - Build a Quiz Game

  • what are you trying to do with this snippet, what is your expected output from it
  • and also try to describe how its deviating from your expected output…

happy coding :slight_smile:

what are question and choice? please describe them. I think you have a misunderstanding there

1 Like

function getResults(question, choice){
let randomChoice = choice[randomNum];
let randomQ = questions[rndmNum].choices;
if(randomQ === randomChoice){
return “The computer’s choice is correct!”
}else{
return "The computer’s choice is wrong. The correct answer is: " + question;
}
}

let randomChoice = choice[randomNum];
let randomQ = questions[rndmNum].choices;

you can’t access these two in function. declare outside of this function . make it global variable

code removed by moderator

// this is my code see if this is helpful for you

1 Like

hi @vinitanjana904

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.

2 Likes

My apologies for that—I’m new to this. I’ll take your feedback seriously and improve.

1 Like

Tell us what’s happening:

What’s wrong with this code? I can’t figure it out.

Your code so far

let questions = [
  {
    category: "name",
    question: "name?",
    choices: ["Jennie","Luisa","Nancy"],
    answer: "Nancy"
  },
  {
    category: "age",
    question: "age?",
    choices: ["58","40","73"],
    answer: "73",
  },
  {
    category: "address",
    question: "address?",
    choices: ["Moon","Mars","Saturn"],
    answer: "Saturn"
  },
  {
    category: "contact preference",
    question: "contact preference?",
    choices: ["email","text","phone call"],
    answer: "text",
  },
  {
    category: "hobbies",
    question: "hobbies?",
    choices: ["water polo","skiing","fishing"],
    answer: "skiing",
  },
]
let randNum = Math.round(Math.random() * questions.length) + 5;
function getRandomquestions(questions){
  return questions[randNum];
}
function getRandomComputerChoice(choices){
  return choices[randNum]
}

function getResults(questions,choices){
  if(questions === choices){
    return "The computer's choice is correct!"
  }else{
    return "The computer's choice is wrong. The correct answer is " + questions
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15

Challenge Information:

Build a Quiz Game - Build a Quiz Game

  • randNum is calculated once, not each time you want a random question.
  • getRandomComputerChoice(choices) tries to use the same randNum which again, is rong

I will add a third point, these two parameters can’t be compared like this, you will need to review what each one of them is

Should i be comparing the answers to the choices, if so how can i do that?

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.

If you are passing the question object and the computer’s choice (singular) you wouldn’t expect them to be equal, ever.

Use console.log() to output your variables to the console, so you can see the values that you’re comparing directly.

Make sure you are passing the correct variables and comparing the correct values.

If the computer choice matches the answer,

You should be comparing the computer choice and the answer.

1 Like

Tell us what’s happening:

I’m still not getting the correct result how can i fix this?
The computer choice array is multi dimensional, i am confused on how to compare answers to computer choices. Any help will be appreciated,
// getResults function
function getResults(question,computerChoice){
question = questions[randomNum].answer;
computerChoice = questions[randomNum].choices
if(question !== computerChoice){
return "The computer’s choice is wrong. The correct answer is: " + question;
}else{
return “The computer’s choice is right!”

}
}

Your code so far

let questions = [
  {
    category: "name",
    question: "name?",
    choices: ["Jennie","Luisa","Nancy"],
    answer: "Nancy"
  },
  {
    category: "age",
    question: "age?",
    choices: ["58","40","73"],
    answer: "73",
  },
  {
    category: "address",
    question: "address?",
    choices: ["Moon","Mars","Saturn"],
    answer: "Saturn"
  },
  {
    category: "contact preference",
    question: "contact preference?",
    choices: ["email","text","phone call"],
    answer: "text",
  },
  {
    category: "hobbies",
    question: "hobbies?",
    choices: ["water polo","skiing","fishing"],
    answer: "skiing",
  },
];
// getRandomQuestions function

function getRandomQuestion(questions){
  let randomQ = Math.round(Math.random() * 5);
  return questions[randomQ]
}

//getRandomComputerChoice function
function getRandomComputerChoice(choices){
  let randomC = Math.round(Math.random() * 5);
  return choices[randomC]
 }


// declare random number variable
let randomNum = Math.round(Math.random() * 5);


// getResults function
function getResults(question,computerChoice){
  question = questions[randomNum].answer;
  computerChoice = questions[randomNum].choices
  if(question !== computerChoice){
    return "The computer's choice is wrong. The correct answer is: " + question;  
  }else{
    return "The computer's choice is right!"
  }
}





  





Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15

Challenge Information:

Build a Quiz Game - Build a Quiz Game

I have merged your duplicate topics, please do not open multiple topics for the same challenge

1 Like

Ok so i realize that i can’t compare the actual question to the computer choice/. But i can’t figure out how to compare the answer from the questions object to the computer choices. Or is there something else I’m missing?

You need to use console.log() in your functions to see what they are doing.

Test your functions.

How many choices are there in each question object?

1 Like

can you show how you did that please?

Here are f the logged items and their results.
let rndmChoice = getRandomComputerChoice(randomC,“\n”)
console.log("random choice: ",rndmChoice)

Random choice: coffee

let correctAnswer = questions[index2].answer
console.log(“\ncorrect answer: “,correctAnswer,”\n”)

Correct answer: sea food

Here is the code for the getResults function:

function getResults(questions,choices){
choices = getRandomComputerChoice(questions[index2].choices);
if(choices == questions[index2].answer){
return “The computers choice is correct!”
}else {
return "The computer’s choice is wrong. The correct answer is: " + answer
}}

Post your full code, please, and format it correctly for the Forum by highlighting your code in the reply and applying </> to it in the editor or by putting three backticks (```) before and after it. That way, it’s so much easier to read.

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').