Random Quote Generator
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
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.
Codeconst 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
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.Codefunction 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.
Hint
Math.floor rounds down, so the index is always between 0 and length-1 — never out of bounds.
- 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.
Codeconst 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
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.
Codelet 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
<blockquote id="quote">Click the button to get a quote!</blockquote>
<button id="new-quote">New Quote</button>
<script>
const quotes = [
/* TODO: add 5 quotes */
];
function randomQuote() {
// TODO: return a random quote
}
// TODO: wire button to show randomQuote()
</script><blockquote id="quote">Click the button to get a quote!</blockquote>
<button id="new-quote">New Quote</button>
<script>
const quotes = [
"Stay curious.",
"Small steps every day.",
"Done is better than perfect.",
"Read the error message.",
"Make it work, then make it good.",
];
function randomQuote() {
const i = Math.floor(Math.random() * quotes.length);
return quotes[i];
}
const quoteEl = document.getElementById("quote");
const btn = document.getElementById("new-quote");
let current = "";
btn.addEventListener("click", () => {
let next = randomQuote();
while (next === current) {
next = randomQuote();
}
current = next;
quoteEl.textContent = next;
});
</script>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
Related Practice
- Array Methods MCQ
Quiz on array access and methods.
- Math Predict
Predict the result of Math.random and Math.floor expressions.
Related Tools
- JSON Formatter
Format the quotes array as JSON to inspect your data structure.
Related Cheatsheets
- JavaScript Cheatsheet
Array, Math, and DOM syntax reference.
Related Reference
- JavaScript Array Methods
Complete array method reference.
- JavaScript Math
Math object reference.
Related Errors
- Cannot Read Properties of Undefined
Undefined element access.
- Reference Error Not Defined
Using undefined variables.
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.
- Cannot Read Properties of Undefined
Undefined element access.
- Reference Error Not Defined
Using undefined variables.