Hello freeCodeCamp community,
I hope you’re all doing well! I’m currently working on the “Build a Quiz Game” lab from the Full Stack Developer certification, and I’m running into some challenges with passing the tests. Even though my code produces the correct output in the console, I’m failing certain test cases with my code. I’d really appreciate any advice or insights from the community to help me figure out what’s going wrong.
Code 01 ↓
const obj1 = {
category: "JavaScript Question",
question: "What is the correct way to declare a variable that cannot be reassigned in JavaScript?",
choices: ["let x = 10;", "const x = 10;", "var x = 10;"],
answer: "const x = 10;"
};
const obj2 = {
category: "CSS Question",
question: "Which CSS property is used to change the text color of an element?",
choices: ["font-style", "color", "text-align"],
answer: "color"
};
const obj3 = {
category: "CSS & HTML Question",
question: "What does the \"viewport\" meta tag in HTML primarily help with?",
choices: ["Setting the font size of the webpage", "Controlling the layout on mobile browsers", "Adding animations to the page"],
answer: "Controlling the layout on mobile browsers"
};
const obj4 = {
category: "JavaScript Question",
question: "Which method is used to add an element to the end of an array in JavaScript?",
choices: ["array.push()", "array.pop()", "array.unshift()"],
answer: "array.push()"
};
const obj5 = {
category: "CSS RWD Question",
question: "In a media query, what does the min-width condition target?",
choices: ["Screens smaller than the specified width", "Screens equal to or larger than the specified width", "Screens exactly at the specified width"],
answer: "Screens equal to or larger than the specified width"
};
const questions = [obj1, obj2, obj3, obj4, obj5];
const getRandomQuestion = (questionsArray) => questionsArray[Math.round(Math.random()*(questionsArray.length - 1))];
let selectedQuestion;
let question;
let randomAnswer;
function getRandomComputerChoice(questionsArray) {
selectedQuestion = questionsArray[Math.round(Math.random()*(questionsArray.length - 1))];
question = `${String(selectedQuestion.category)} → ${String(selectedQuestion.question)}`;
randomAnswer = (selectedQuestion.choices)[Math.round(Math.random()*((selectedQuestion.choices).length - 1))];
return `Selected Question: ${String(question)}
Answer of the Computer: ${String(randomAnswer)}`;
}
function getResults(questionsArray) {
console.log(getRandomComputerChoice(questionsArray));
if (selectedQuestion.answer == randomAnswer) {
return "The computer's choice is correct!";
} else {
return `The computer's choice is wrong. The correct answer is: ${String(selectedQuestion.answer)}`;
}
}
console.log(getResults(questions));
Console Output ↓
// running tests
9. You should have a function named getRandomComputerChoice that takes the array of the available choices as a parameter, and returns a random answer to the selected question.
11. If the computer choice matches the answer, getResults should return The computer's choice is correct!
12. If the computer choice doesn't match the answer, getResults should return The computer's choice is wrong. The correct answer is: <correct-answer>, where <correct-answer> is the value of the correct answer to the chosen question.
// tests completed
// console output
Selected Question: JavaScript Question → Which method is used to add an element to the end of an array in JavaScript?
Answer of the Computer: array.pop()
The computer's choice is wrong. The correct answer is: array.push()
Thank you so much for taking the time to read this and for any help you can offer! I’m excited to get this working and move forward with the lab.
Best regards,
@HimalErangana