Tell us what’s happening:
I felt like it’s already fulfill its goal. However, seems like I had miss somethings? I know that the fact that I can’t just put function inside the function params without declaring it as a variable. So, do anyone have any Idea?
Your code so far
const questions = [{
category: "Easy",
question: "What is 1+1?",
choices: ["2","Two","Window"],
answer:"2"
},{
category: "Medium",
question: "Why is sun rising from the east?",
choices: ["Orbit","It is what it is","idk"],
answer:"Orbit"
},{
category: "Hard",
question: "Why is duck mostly have darker feather?",
choices: ["Water","Sun","idk"],
answer:"Sun"
},{
category: "HARDCORE",
question: "Why is 727 became an osu! culture?",
choices: ["Cookiezi","BTMC said Cookiezi","Cookiezee"],
answer:"Cookiezi"
},{
category: "KASAIIIII",
question: "ASAHI???",
choices: ["PULU","ASAHI","TENG"],
answer:"ASAHI"
}];
function getRandomQuestion(arr){
return arr[(Math.ceil(Math.random()*arr.length-1))];
};
function getRandomComputerChoice(arr){
return arr.choices[Math.ceil(Math.random()*arr.choices.length-1)];
}
function getResults(arr, randAns){
if (randAns === arr.answer){
return `The computer's choice is correct!`;
} else if (randAns !== arr.answer) {
return `The computer's choice is wrong. The correct answer is: ${selectedQuestion.answer}.`;
};
};
const selectedQuestion = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(selectedQuestion);
console.log(`Question: ${selectedQuestion.question}`);
console.log(`Computer picked: ${computerChoice}`);
console.log(getResults(selectedQuestion, computerChoice));
Challenge Information:
Build a Quiz Game - Build a Quiz Game
hello @juliusechndr welcome to the forum!
If arr is an array, what should you get if you log arr.choices? Currently your function is expecting an object with a choices property.
You should remove the last full-stop here.
const questions = [{
category: "Easy",
question: "What is 1+1?",
choices: ["2","Two","Window"],
answer:"2"
},{
category: "Medium",
question: "Why is sun rising from the east?",
choices: ["Orbit","It is what it is","idk"],
answer:"Orbit"
},{
category: "Hard",
question: "Why is duck mostly have darker feather?",
choices: ["Water","Sun","idk"],
answer:"Sun"
},{
category: "HARDCORE",
question: "Why is 727 became an osu! culture?",
choices: ["Cookiezi","BTMC said Cookiezi","Cookiezee"],
answer:"Cookiezi"
},{
category: "KASAIIIII",
question: "ASAHI???",
choices: ["PULU","ASAHI","TENG"],
answer:"ASAHI"
}];
function getRandomQuestion(arr){
return arr[(Math.ceil(Math.random()*arr.length-1))];
};
const randomQuestion = getRandomQuestion(questions);
function getRandomComputerChoice(arr=randomQuestion){
return arr.choices[Math.ceil(Math.random()*arr.choices.length-1)];
};
function getResults(arr, randAns){
if (randAns === arr.answer){
return `The computer's choice is correct!`;
} else if (randAns !== arr.answer) {
return `The computer's choice is wrong. The correct answer is: ${selectedQuestion.answer}`;
};
};
const selectedQuestion = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(selectedQuestion);
console.log(`Question: ${selectedQuestion.question}`);
console.log(`Computer picked: ${computerChoice}`);
console.log(getResults(selectedQuestion, computerChoice));
So here, I’ve store the random question an variable and it seems to fix a check. Now I miss three checks. I don’t really understand what do I miss here. I also remove that full-stop on my else if, thank you for your notes.
Thank you for your help @sampatee. Looking forward for my coding journey in this wholesome community.