Build a Quiz Game - Build a Quiz Game

let questions=[
{
 category:"color? ", 
 question:"what is your favorite color?",
 choices:["blue","pink","green"],
 answer:"green",
},
{
 category:"living place?", 
 question:"where are you living there ?",
 choices:["Bursa","Ankara","Istanbul"],
 answer:"Istanbul",
},
{
 category:"Field of study?", 
 question:"Do you grageuated?",
 choices:["computer","Math","teacher"],
 answer:"computer",
},
{
   category:"cooking?", 
 question:"What is your favorite food?",
 choices:["Pizza","Ege","Rice"],
 answer:"Ege",
},
{
 category:"sports?", 
 question:"What is your favorite sport?",
 choices:["football", "volleyball","swimming"],
 answer:"volleyball",
}
];
function getRandomQuestion(questions) {
    let randomQuestion = Math.floor(Math.random() * questions.length);
    return questions[randomQuestion];
};

//FUNCTION CALL
let gRandomQuestion = getRandomQuestion(questions);

function getRandomComputerChoice(gRandomQuestion) {
    let random = Math.floor(Math.random() * gRandomQuestion.length);
    return gRandomQuestion[random];
};
//FUNCTION CALL
let computerChoice = getRandomComputerChoice(gRandomQuestion.choices);
//console.log(computerChoice);

let rightAnswer = gRandomQuestion.answer;
//console.log(rightAnswer);

function getResults(pcChoice,truAnswer) {

   return pcChoice == truAnswer? "The computer's choice is correct!" : `The computer's choice is wrong. The correct answer is: ${truAnswer}`

};

console.log(getResults(computerChoice,rightAnswer));

my program work , but the test 11 and 12 not passed. please help what is the problem

what is test 11 asking from you?

The problem is that the instructions ask you to pass the selected question object to the function but instead you are passing the answer property.

function getResults(pcChoice,answer) {
   return pcChoice == answer? "The computer's choice is correct!" : `The computer's choice is wrong. The correct answer is: ${answer}`};
//FUNCTION CALL
let gRandomQuestion = getRandomQuestion(questions);
let computerChoice = getRandomComputerChoice(gRandomQuestion.choices);
let result=getResults(computerChoice,gRandomQuestion.answer);
console.log(result);

Do you mean like this?

Is this the question object or the answer property?

You should have a function named getResults that takes the selected question object and the computer choice as its parameters

the answer property. if we send the question object us argument of getResults function i edited the code us bellow. but stil not passed.

function getResults(pcChoice,gRandomQuestion) {
    let answer=gRandomQuestion.answer;
   return pcChoice == answer? "The computer's choice is correct!" : `The computer's choice is wrong. The correct answer is: ${answer}`};
//FUNCTION CALL
let gRandomQuestion = getRandomQuestion(questions);
let computerChoice = getRandomComputerChoice(gRandomQuestion.choices);
let result=getResults(computerChoice,gRandomQuestion);
console.log(result);

I hope you can guess my next question.

Is gRandomQuestion a question object? How can you check?

Does the getRandomQuestion return an object?

To be clear of the difference, this is an object:

{
 category:"color? ", 
 question:"what is your favorite color?",
 choices:["blue","pink","green"],
 answer:"green",
}

This is a property:

answer:"green"

yes, gRandomQuestion is an object of questions array that is returned from
getRandomQuestion functions.
getRandomQuestion return and object.

Ok can you show me why? I disagree with you, so can you prove it?

EDIT: Ok I see you’ve updated your code, it looks like getRandomQuestion should be returning an object. :+1:

Can you post your full code update with the new getResults() function as well, so I can see it all together?

This is an annoying one:

You should have a function named getResults that takes the selected question object and the computer choice as its parameters

function getResults(pcChoice,gRandomQuestion) {

Your function takes the choice, and then the question.

1 Like
let questions=[
{
 category:"color? ", 
 question:"what is your favorite color?",
 choices:["blue","pink","green"],
 answer:"green",
},
{
 category:"living place?", 
 question:"where are you living?",
 choices:["Bursa","Ankara","Istanbul"],
 answer:"Istanbul",
},
{
 category:"Field of study?", 
 question:"Do you grageuated?",
 choices:["computer","Math","teacher"],
 answer:"computer",
},
{
   category:"cooking?", 
 question:"What is your favorite food?",
 choices:["Pizza","Ege","Rice"],
 answer:"Ege",
},
{
 category:"sports?", 
 question:"What is your favorite sport?",
 choices:["football", "volleyball","swimming"],
 answer:"volleyball",
}
];
function getRandomQuestion(questions) {
    let randomQuestionObj = Math.floor(Math.random() * questions.length);
    return questions[randomQuestionObj];
};
function getRandomComputerChoice(gRandomQuestion) {
    let random = Math.floor(Math.random() *gRandomQuestion.length);
    return gRandomQuestion[random];
};

function getResults(pcChoice,gRandomQuestion) {
    let answer=gRandomQuestion.answer;
   return pcChoice == answer? "The computer's choice is correct!" : `The computer's choice is wrong. The correct answer is: ${answer}`};
//FUNCTION CALL
let gRandomQuestion = getRandomQuestion(questions);
let computerChoice = getRandomComputerChoice(gRandomQuestion.choices);
let result=getResults(computerChoice,gRandomQuestion);
console.log(result);

this is my full code.

you still have the same issue pointed by pkdvalis above

1 Like

my problem was the order of parameter.
thank you :pray:

1 Like

Please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.