Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

if the questions array and choices array had the same length my program were true, but it is not what should i do?

Your code so far

let questions=[
{
 category:"color? ", 
 question:"what is your favorite color?",
 choices:["blue","pink","green"],
 answer:"green",
},
{
 category:"living place?", 
 question:"do you like 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",
}

];
let random = Math.floor(Math.random() * questions.length);
function getRandomQuestion(questions) {
    let randomQuestion = random;
    return questions[randomQuestion].question;
};
//FUNCTION CALL
let gRandomQuestion = getRandomQuestion(questions);
let finduserAnswer = questions.find(user => user.question === gRandomQuestion);
let userAnswer = finduserAnswer.answer;


function getRandomComputerChoice(choices) { 
   return choices[random];
};
//FUNCTION CALL
let computerChoice = getRandomComputerChoice(questions[random].choices);

//result Function
function getResults() {
    if (computerChoice == userAnswer) {
        return "The computer's choice is correct!";
    }
    else {
        return `The computer's choice is wrong. The correct answer is: ${userAnswer}.}`;
    }
};
console.log(getResults());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

I am not sure I understand, whad do you think is the issue?

what is the problem of my code?

check the output string

if you don’t see anything wrong double check it, triple check it

i checked it, but test number 8,11,12 not passed

And you are printing to the console The computer's choice is wrong. The correct answer is: volleyball.} are you sure there is anything wrong with this string? isn’t there something extra?

. and } are extra, but still not work

now that you have removed that, let’s take a look at the getRandomQuestion function.

If you call that function multiple times, do you expect to get different questions or always the same one? ahve you tried calling it multiple times?

let random = Math.floor(Math.random() * questions.length);
function getRandomQuestion(questions) {
    let randomQuestion = random;
    return questions[randomQuestion].question;
};

console.log(getRandomQuestion(questions));
console.log(getRandomQuestion(questions));
console.log(getRandomQuestion(questions));
2 Likes

I made some changes, but still not passed .

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.choices.length);
    return gRandomQuestion.choices[random];
};
//FUNCTION CALL
let computerChoice = getRandomComputerChoice(gRandomQuestion);

let rightAnswer = gRandomQuestion.answer;
function getResults() {
    if (computerChoice === rightAnswer) {
        return "The computer's choice is correct!";
    }
    else {
        return `The computer's choice is wrong. The correct answer is: ${rightAnswer}`;
        
    }
};

console.log(getResults());

how is it going? do you need more help?

Tell us what’s happening:

The last sections are not passed. Can help please?

Your code so far

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() {

    if(computerChoice==rightAnswer) {
        return "The computer's choice is correct!";
    }
    else {
        return `The computer's choice is wrong.The correct answer is: ${rightAnswer}`;
        
    }
};

console.log(getResults());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Did you do this part?

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

item 9 from the user stories

function getResults(pcChoice,truAnswer) {

    if(pcChoice==truAnswer) {
        return "The computer's choice is correct!";
    }
else {
    return `The computer's choice is wrong.The correct answer is: ${truAnswer}`;
     } 
};

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

yes i did

the getResults function still not passed ,can you help what is the problem?

Is rightAnswer the selected question object?

Here is the Full Code:

  • removed by mod

yes it is the answer property that selected manually by user.

Is rightAnswer the selected question object?

getResults that takes the selected question object

Please do not post solution code to the forum

1 Like