Skip to content

Random Quote Generator

Easy~15 min

What you'll build

A page with a quote display area and a "New quote" button. Each click picks a random quote from a built-in array and shows it on the page.

Goals

  • Store data in an array of strings
  • Generate a random index with Math.random and Math.floor
  • Update DOM text from an event handler
  • Avoid repeating the same quote twice in a row

Prerequisites

  • Arrays and index access
  • Functions
  • DOM selection and event listeners

Steps

  1. 1

    Store the quotes

    Create an array of at least five short quotes. Keeping the data separate from the logic makes it easy to add more later.

    Code
    const quotes = [
      "Stay curious.",
      "Small steps every day.",
      "Done is better than perfect.",
      "Read the error message.",
      "Make it work, then make it good.",
    ];

    Expected result

    An array of five strings is ready in memory.

  2. 2

    Pick a random quote

    Write a function randomQuote() that returns a random element. Math.random() gives a number in [0,1); multiply by the array length and floor it to get a valid index.

    Code
    function randomQuote() {
      const i = Math.floor(Math.random() * quotes.length);
      return quotes[i];
    }

    Expected result

    Calling randomQuote() returns one of the five strings, different each time.

  3. 3

    Wire the button

    Select the quote element and the button. On click, call randomQuote() and set the element's textContent. This connects the data to the UI.

    Code
    const quoteEl = document.getElementById("quote");
    const btn = document.getElementById("new-quote");
    
    btn.addEventListener("click", () => {
      quoteEl.textContent = randomQuote();
    });

    Expected result

    Each click shows a new random quote on the page.

  4. 4

    Avoid repeating the same quote

    Keep track of the current quote and, if the new one matches, pick again. This small loop guarantees the user always sees a change when they click.

    Code
    let current = "";
    btn.addEventListener("click", () => {
      let next = randomQuote();
      while (next === current) {
        next = randomQuote();
      }
      current = next;
      quoteEl.textContent = next;
    });

    Expected result

    Clicking never shows the same quote twice in a row.

Try it yourself

What you learned

You combined an array, random indexing, and a DOM event handler to build a generator — and added a small guard so the output always changes, a common real-world polish.

Related Resources

Related Lessons

  • Arrays

    Array indexing and the length property used to pick a random quote.

  • DOM

    The click listener that triggers a new quote.

Related Practice

Related Tools

  • JSON Formatter

    Format the quotes array as JSON to inspect your data structure.

Related Cheatsheets

Related Reference

Related Errors

Related Glossary

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Related Errors

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

← Back to topic