Tell us what’s happening:
Hey,
My code is passing sometimes and failing the other. Obviously to do with the Math. Random I’ve possibly messed up. Can someone help me decipher what’s causing this?
It might also be because its very late but I am not seeing where the issue lies. Assuming its within computer choice not having the correct search parameter or return.
Your code so far
const questions = [];
const q1 = {
category: "Colour",
question: "Whats colour's best?",
choices: ["Cyan", "Teal", "Pink"],
answer: "Teal"
};
const q2 = {
category: "Hate",
question: "Who do you hate more?",
choices: ["Boss", "Kids", "Java"],
answer: "Boss"
};
const q3 = {
category: "Home",
question: "Will you buy a house?",
choices: ["Maybe", "Yes", "No"],
answer: "Yes"
};
const q4 = {
category: "X-Men",
question: "Who is the best X-Men?",
choices: ["Iceman", "Magic", "Sunspot"],
answer: "Iceman"
};
const q5 = {
category: "Games",
question: "What one do you like more?",
choices: ["Bloodborne", "FF14", "Marvel"],
answer: "Bloodborne"
};
questions.push(q1, q2, q3, q4, q5);
function getRandomQuestion(questions) {
let randomQ = Math.floor(Math.random() * 5);
return questions[randomQ];
};
function getRandomComputerChoice (choices) {
let randomC = Math.floor(Math.random() * 5);
return choices[randomC];
};
function getResults(question, getRandomComputerChoice) {
if (getRandomComputerChoice == question.answer) {
return "The computer's choice is correct!";
} else {
return `The computer's choice is wrong. The correct answer is: ${question.answer}`;
};
};
console.log(getResults(q4));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game