daiwik
1
Tell us what’s happening:
Why isn’t my getResults working. I am confused. any help is appreciated
Your code so far
let questions = []
let obj1 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "a"
}
let obj2 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "b"
}
let obj3 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "c"
}
let obj4 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "a"
}
let obj5 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "b"
}
questions = [obj1,obj2,obj3,obj4,obj5]
function getRandomQuestion(array){
let randomQuestion = Math.floor(Math.random() * (array.length));
return array[randomQuestion]
}
console.log(getRandomQuestion(questions))
function getRandomComputerChoice(array){
let randomChoice = Math.floor(Math.random() * (array.length));
return array[randomChoice];
}
console.log(getRandomComputerChoice(obj1.choices))
function getResults(questionObj,computerChoice){
let randomChoice = getRandomComputerChoice(questionObj.choices) ;
if(randomChoice === computerChoice){
return "The computer's choice is correct!"
} else {
return `The computer's choice is wrong. The correct answer is: ${randomChoice}`
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game
GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-quiz-game/66f17db06803d11a1bd19a20.md at main · freeCodeCamp/freeCodeCamp · GitHub
Teller
2
Hi @daiwik
Why is the same set in inputs getting different outputs?
Happy coding
daiwik
3
I think this is the right way but I am still not passing getResults()
let questions = []
let obj1 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "a"
}
let obj2 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "b"
}
let obj3 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "c"
}
let obj4 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "a"
}
let obj5 = {
category: "a?",
question: "b?",
choices: ["a","b","c"],
answer: "b"
}
questions = [obj1,obj2,obj3,obj4,obj5]
function getRandomQuestion(array){
let randomQuestion = Math.floor(Math.random() * (array.length));
return array[randomQuestion]
}
console.log(getRandomQuestion(questions))
function getRandomComputerChoice(array){
let randomChoice = Math.floor(Math.random() * (array.length));
return array[randomChoice];
}
console.log(getRandomComputerChoice(obj1.choices))
function getResults(questionObj,computerChoice){
if(questionObj.answer === computerChoice){
return "The computer's choice is correct!";
} else {
return `The computer's choice is wrong. The correct answer is: ${randomChoice}`
}
}
console.log(getResults(obj1,"a"));
Teller
4
Hi @daiwik
Nearly there. This this function call:
console.log(getResults(obj1,"b"));
Happy coding
daiwik
5
have to change randomChoice to questionObj.answer
Thank you this completed the challenge