- Nov 6 2024
Music Coding Challenge
Are you a Rock N Roll Coding Star? 🎸
Prove it with our music quiz!
Either choose to test yourself, or use our quiz as a template to make your own.
Just copy the code block below and paste it into your console.
*To open your console on Google Chrome, just press command+option+J*
Good luck!
CS 🎸
// Welcome to the Quiz
alert("Welcome to our 90s music quiz! To answer the questions, simply answer using the number. For example: 1");
// Array of quiz questions
const quizQuestions = [
{
question: "1. What is the name of the 90s Britpop band led by the Gallagher brothers?",
choices: ["1. Oasis", "2. Blur", "3. Suede"],
correctAnswer: 0
},
{
question: "2. Who sings Wannabe?",
choices: ["1. Sugababes", "2. Spice Girls", "3. Saturdays"],
correctAnswer: 1
},
{
question: "3. Which is a Queen song?",
choices: ["1. Bohemian Rhapsody", "2. Wonderwall", "3. Heroes"],
correctAnswer: 0
}
];
// Function to run the quiz
function runQuiz() {
let score = 0;
// Loop through each question
for (let i = 0; i < quizQuestions.length; i++) {
let userAnswer;
// Validate the user's input
do {
userAnswer = prompt(quizQuestions[i].question + "\n" + quizQuestions[i].choices.join("\n"));
} while (!userAnswer || isNaN(userAnswer) || userAnswer < 1 || userAnswer > quizQuestions[i].choices.length);
// Convert the user's input to an integer and adjust for zero-based index
userAnswer = parseInt(userAnswer) - 1;
// Check if the answer is correct
if (userAnswer === quizQuestions[i].correctAnswer) {
score++;
alert("Correct!");
} else {
alert("Wrong! The correct answer is: " + quizQuestions[i].choices[quizQuestions[i].correctAnswer]);
}
}
// Display the final score
alert("Quiz Complete! Congratulations! You scored " + score + " out of " + quizQuestions.length);
}
// Start the quiz
runQuiz();
Ready to launch your tech career?