The following code returns the two expected outcomes randomly. I’m unsure what I’m messing up on.
const questions = []
let question1 = {
category: "html",
question: "Question 1?",
choices: ["Choice 1", "Choice 2", "answer"],
answer: "answer"
};
let question2 = {
category:"CSS",
question:"Question 2?",
choices: ["Choice 1", "Choice 2", "answer"],
answer: "answer"
}
let question3 = {
category:"Void Elements",
question:"Question 3?",
choices: ["Choice 1", "Choice 2", "answer"],
answer: "answer"
}
let question4 = {
category:"Strings",
question:"Question 4?",
choices: ["Choice 1", "Choice 2", "answer"],
answer: "answer"
}
let question5 = {
category:"Arrays",
question:"Question 5?",
choices: ["Choice 1", "Choice 2", "answer"],
answer: "answer"
}
questions.push(question1, question2, question3, question4, question5,);
let qIndex = Math.floor(Math.random() * questions.
length);
function getRandomQuestion(questions){
return questions[qIndex];
};
function getRandomComputerChoice(choices){
let rccIndex = Math.floor(Math.random() * questions[qIndex].choices.length);
return questions[qIndex].choices[rccIndex];
}
console.log()
let qObject = getRandomQuestion(questions);
let cChoice = getRandomComputerChoice()
function getResults(qObject, cChoice){
if(qObject.answer === cChoice){
return "The computer's choice is correct!";
}else{
return `The computer's choice is wrong. The correct answer is: ${qObject.answer}`
}
};
console.log(getResults(qObject, Choice));