DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Book Quote Generator App with JavaScript

A quote generator is a perfect beginner project: it teaches arrays, random selection, DOM updates, and event handling in one small app. And a book-quote version is genuinely useful — a new idea from a great book every time you click a button.

Start with the data. An array of quote objects, each with the quote, the author, and the book it came from:

const quotes = [
  { text: 'The best investment you can make is in yourself.', author: 'Warren Buffett', book: 'The Essays of Warren Buffett' },
  { text: 'You do not rise to the level of your goals. You fall to the level of your systems.', author: 'James Clear', book: 'Atomic Habits' },
  { text: 'The rich buy assets. The poor buy liabilities.', author: 'Robert Kiyosaki', book: 'Rich Dad Poor Dad' },
];
Enter fullscreen mode Exit fullscreen mode

Then pick one at random and show it. Math.random() gives a number from 0 to 1, so multiply it by the array length and round down:

function showRandomQuote() {
  const index = Math.floor(Math.random() * quotes.length);
  const quote = quotes[index];
  document.querySelector('#quote-text').textContent = `"${quote.text}"`;
  document.querySelector('#quote-author').textContent = `— ${quote.author}, ${quote.book}`;
}

document.querySelector('#new-quote').addEventListener('click', showRandomQuote);
showRandomQuote(); // show one on page load
Enter fullscreen mode Exit fullscreen mode

Add a copy button with the Clipboard API so users can share a quote they like, and a small filter — a dropdown to show only quotes from one book, using Array.filter. That turns a toy project into something people actually use.

If a quote makes you curious about the book it came from, this list of best business books for beginners is a good place to start reading — most of the famous quotes in business come from just a handful of books on that list.

Finish with a little polish: fade the quote in with a CSS transition when it changes, and avoid showing the same quote twice in a row by remembering the last index. Small details like these are what make an app feel finished instead of feeling like homework.

Build this in an afternoon, and you will have practised arrays, random numbers, events, and the DOM — the four skills behind almost every beginner project.

Top comments (0)