Tell us what’s happening:
The 2nd object in the questions array’s choices are [“”, “
Your code so far
const questions = [
{
category: "JavaScript",
question: "Which keyword is used to declare a variable that cannot be reassigned?",
choices: ["let", "const", "var"],
answer: "const"
},
{
category: "HTML",
question: "Which tag is used to create a hyperlink?",
choices: ["<a>", "<p>", "<img>"],
answer: "<a>"
},
{
category: "CSS",
question: "Which property is used to change text color?",
choices: ["font-size", "color", "background-color"],
answer: "color"
},
{
category: "Programming",
question: "What does JSON stand for?",
choices: ["Java Source Object Notation", "JavaScript Object Notation", "Java Structured Output Network"],
answer: "JavaScript Object Notation"
},
{
category: "Web",
question: "Which of these is a JavaScript framework/library?",
choices: ["React", "Photoshop", "MySQL"],
answer: "React"
}
];
function getRandomQuestion(arr){
return arr[Math.floor(Math.random() * 4)];
}
function getRandomComputerChoice(arrChoices){
return arrChoices[Math.floor(Math.random() * 3)];
}
function getResults(questionObj, computerChoice){
if(computerChoice === questionObj.answer){
return "The computer's choice is correct!";
} else if (getRandomComputerChoice(computerChoice) !== questionObj.answer){
return `The computer's choice is wrong. The correct answer is: ${questionObj.answer}`;
}
}
const randomObjq = getRandomQuestion(questions);
const randomc = getRandomComputerChoice(randomObjq.choices);
console.log(`The Object picked: ${JSON.stringify(randomObjq, null, 2)}`);
console.log(`The answer picked: ${randomc}`);
console.log(getResults(randomObjq, randomc));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game