Avatar
Joe Stephens
  • Oct 30 2024

Halloween Coding Challenge

Happy Halloween!

If you're in need of some spooky coding fun, then why don't you try out our Halloween coding 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 on Mac, or Ctrl+shift+J*

Happy Halloween! 🎃 

// Welcome to the Quiz

alert("Welcome to our Halloween quiz! To answer the questions, simply press the corresponding number. For example: 1");

// Array of quiz questions

const quizQuestions = [

    {

        question: "What are the names of the witches in Hocus Pocus?",

        choices: ["1. Winifred, Mary, Sarah", "2. Santa, Rudolph, Comet", "3. Meg, Mog, Kirsty"],

        correctAnswer: 0

    },

    {

        question: "2. What do you call a group of witches?",

        choices: ["1. A band", "2. A Coven", "3. A broom"],

        correctAnswer: 1

    },

    {

        question: "3. Which line finishes this verse: 'If there's something strange, in your neighbourhood. Who you gonna call?' ?",

        choices: ["1. Ghostbusters!", "2. Monster Mash!", "3. The pumpkin!"],

        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 + " Have a spooky Halloween!🎃");

}

// Start the quiz

runQuiz();

Ready to launch your tech career?

Join Command Shift 💚
Command Shift
© 2024 MCRCODES LTD. All rights reserved.