Project 3:
Simple Quiz Application



Overview

This project demonstrates how to build a simple quiz application using pure JavaScript. It runs entirely in the browser console or Node.js environment and does not depend on HTML or browser DOM. It's perfect for beginners learning how to use arrays, functions, conditionals, loops, and basic input/output operations.

Objective

Create a JavaScript-based quiz program that:

Step-by-Step Development

Step 1: Define Quiz Questions

Start with an array of objects to represent each quiz question.


const questions = [
  {
    question: "Which language runs in a web browser?",
    options: ["Java", "C", "Python", "JavaScript"],
    answer: "JavaScript"
  },
  {
    question: "What does CSS stand for?",
    options: ["Central Style Sheets", "Cascading Style Sheets", "Cascading Simple Sheets", "Cars SUVs Sailboats"],
    answer: "Cascading Style Sheets"
  },
  {
    question: "What year was JavaScript created?",
    options: ["1996", "1995", "1994", "none of the above"],
    answer: "1995"
  }
];
    

Why use objects to store questions?

Objects let us bundle related data (question, options, answer) together. This makes it easy to loop over them and access the needed information cleanly.

Step 2: Ask Questions and Get User Input

In Node.js, use readline to read input from the terminal.


const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

let score = 0;
let current = 0;

function askQuestion() {
  const q = questions[current];
  console.log("\n" + q.question);
  q.options.forEach((opt, idx) => {
    console.log(`${idx + 1}. ${opt}`);
  });

  readline.question("Your answer (1-4): ", (userInput) => {
    const chosen = q.options[parseInt(userInput) - 1];
    if (chosen === q.answer) {
      console.log("✅ Correct!");
      score++;
    } else {
      console.log(`❌ Wrong! The correct answer is: ${q.answer}`);
    }

    current++;
    if (current < questions.length) {
      askQuestion();
    } else {
      console.log(`\n🎉 Quiz Finished! Your Score: ${score}/${questions.length}\n`);
      readline.close();
    }
  });
}

askQuestion();
    

Output:

Which language runs in a web browser?
1. Java
2. C
3. Python
4. JavaScript
Your answer (1-4): 4
✅ Correct!

...

🎉 Quiz Finished! Your Score: 2/3
    

How does the input handling work?

The readline module listens for input in the terminal. Each time the user answers a question, we compare their choice to the correct answer and update the score accordingly.

Step 3: Enhancements (Optional)

Final Thoughts

This project teaches important JavaScript concepts in a real-world context. By using arrays, loops, input/output handling, and basic conditional logic, you can build a complete functional application—all in JavaScript.

Questions to Reinforce Learning

Q1: Why do we use parseInt() on user input?

A: Because input from readline is always a string. We convert it to a number so we can use it to index the options array.

Q2: What would happen if the user enters a number out of range (e.g., 5)?

A: The chosen variable would be undefined, and the answer comparison would fail. We can add a validation to handle this gracefully.

Q3: Can this code run in a browser?

A: No. The readline module is Node.js specific. For browser-based quizzes, you'd use prompt() or create UI with HTML and handle events with JavaScript.

Key Takeaways



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M