Lesson 7.4 β Random Quote Generator
π Building a Random Quote Generator Using HTML, CSS & JavaScript
π― Learning Objectives
After completing this lesson, students will be able to:
β Create a Random Quote Generator
β Understand Arrays
β Generate Random Numbers
β Use Math.random()
β Use Functions
β Use DOM Manipulation
β Build Interactive JavaScript Projects
π Introduction
Many websites display motivational quotes.
Examples:
Success is not final, failure is not fatal.
Never stop learning.
Dream big and work hard.
Every time the user clicks a button:
A new quote appears.
Today we will create:
π¬ Random Quote Generator
π Project Preview
-------------------------
Random Quote Generator
"Dream Big, Work Hard"
[ New Quote ]
-------------------------
When the button is clicked:
A new random quote appears.
π― Concepts Used
| Concept | Used |
|---|---|
| Arrays | β |
| Variables | β |
| Functions | β |
| Events | β |
| DOM Manipulation | β |
| Random Numbers | β |
ποΈ Project Files
Create:
index.html
style.css
script.js
π― Step 1 β Create HTML Structure
index.html
<!DOCTYPE html>
<html>
<head>
<title>Random Quote Generator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>
Random Quote Generator
</h1>
<p id="quote">
Click the button to generate a quote
</p>
<button onclick="generateQuote()">
New Quote
</button>
</div>
<script src="script.js"></script>
</body>
</html>
π Understanding HTML
Quote Area:
<p id="quote">
Displays quotes.
Button:
<button onclick="generateQuote()">
Generates a new quote.
π― Step 2 β Design the Project
style.css
body{
font-family:Arial;
background:#f4f4f4;
display:flex;
justify-content:center;
align-items:center;
height:100vh;
}
.container{
background:white;
padding:30px;
width:500px;
text-align:center;
border-radius:10px;
box-shadow:0 0 15px lightgray;
}
h1{
color:#0d6efd;
}
#quote{
font-size:22px;
margin:20px 0;
color:#333;
}
button{
padding:10px 20px;
background:#0d6efd;
color:white;
border:none;
border-radius:5px;
cursor:pointer;
}
button:hover{
background:#084298;
}
π― Step 3 β What is an Array?
An Array stores multiple values in a single variable.
Example:
let fruits =
["Apple","Mango","Banana"];
Output:
Apple
Mango
Banana
stored in one variable.
π Creating Quotes Array
script.js
let quotes =
[
"Dream Big and Work Hard",
"Success Comes Through Consistency",
"Never Stop Learning",
"Believe In Yourself",
"Stay Positive Stay Strong",
"Practice Makes Perfect",
"Every Day Is A New Opportunity",
"Focus On Your Goals"
];
π― Understanding Array Index
quotes[0]
Output:
Dream Big and Work Hard
quotes[1]
Output:
Success Comes Through Consistency
quotes[2]
Output:
Never Stop Learning
π Generating Random Numbers
JavaScript provides:
Math.random()
Example:
Math.random();
Output:
0.52
0.84
0.12
0.67
Random every time.
π― Generate Random Array Index
Math.floor(
Math.random() * quotes.length
);
Suppose:
quotes.length = 8
Output:
0
1
2
3
4
5
6
7
Randomly.
π Create Function
function generateQuote()
{
}
Runs when button is clicked.
π― Generate Random Index
let randomIndex =
Math.floor(
Math.random() * quotes.length
);
Example Output:
3
π Get Quote From Array
let randomQuote =
quotes[randomIndex];
Example:
Believe In Yourself
π― Display Quote
document.getElementById("quote").innerHTML =
randomQuote;
Output:
Random quote appears.
π Complete JavaScript Code
script.js
let quotes =
[
"Dream Big and Work Hard",
"Success Comes Through Consistency",
"Never Stop Learning",
"Believe In Yourself",
"Stay Positive Stay Strong",
"Practice Makes Perfect",
"Every Day Is A New Opportunity",
"Focus On Your Goals"
];
function generateQuote()
{
let randomIndex =
Math.floor(
Math.random() * quotes.length
);
let randomQuote =
quotes[randomIndex];
document.getElementById("quote").innerHTML =
randomQuote;
}
π» Complete Project Code
index.html
<!DOCTYPE html>
<html>
<head>
<title>Random Quote Generator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>
Random Quote Generator
</h1>
<p id="quote">
Click the button to generate a quote
</p>
<button onclick="generateQuote()">
New Quote
</button>
</div>
<script src="script.js"></script>
</body>
</html>
script.js
let quotes =
[
"Dream Big and Work Hard",
"Success Comes Through Consistency",
"Never Stop Learning",
"Believe In Yourself",
"Stay Positive Stay Strong",
"Practice Makes Perfect",
"Every Day Is A New Opportunity",
"Focus On Your Goals"
];
function generateQuote()
{
let randomIndex =
Math.floor(
Math.random() * quotes.length
);
let randomQuote =
quotes[randomIndex];
document.getElementById("quote").innerHTML =
randomQuote;
}
π― Expected Output
Click Button:
New Quote
Output Example:
Dream Big and Work Hard
Next Click:
Stay Positive Stay Strong
Next Click:
Never Stop Learning
π Add Quote Author
Modify Array:
let quotes =
[
"Dream Big and Work Hard - Shiv",
"Believe In Yourself - APJ Abdul Kalam",
"Never Stop Learning - Unknown",
"Stay Positive - Motivational Quote"
];
Output:
Believe In Yourself - APJ Abdul Kalam
π Add Random Background Color
let colors =
["red","blue","green","purple","orange"];
Inside Function:
document.body.style.backgroundColor =
colors[
Math.floor(
Math.random()*colors.length
)
];
Each quote changes background color.
π« Real World Applications
Random Quote Generators are used in:
β Motivational Websites
β Learning Platforms
β Mobile Apps
β Wellness Apps
β Productivity Tools
β Educational Websites
β οΈ Common Beginner Mistakes
Mistake 1
Forgetting Array Brackets
Wrong:
let quotes =
"Quote 1",
"Quote 2";
Correct:
let quotes =
[
"Quote 1",
"Quote 2"
];
Mistake 2
Wrong Element ID
document.getElementById("quote")
Must match HTML ID.
Mistake 3
Missing Math.floor()
Can create decimal indexes.
Wrong:
quotes[Math.random()*quotes.length]
Correct:
quotes[
Math.floor(
Math.random()*quotes.length
)
]
π Best Practices
β Use meaningful quotes.
β Keep array organized.
β Use proper variable names.
β Test random generation multiple times.
β Separate HTML, CSS, and JavaScript files.
π Project Challenge
Modify the project and add:
β Quote Author
β Random Background Colors
β Previous Quote Button
β Quote Counter
β Auto Quote Change Every 5 Seconds
π Practical Activity
Create:
Study Motivation Generator
Add 10 motivational quotes for students.
Display a random quote every time the button is clicked.
π Assignment
Assignment 7.4
- What is an Array?
- What does
Math.random()do? - What does
Math.floor()do? - How does the Random Quote Generator work?
- Create a Quote Generator project.
- Add Quote Authors.
- Add Random Background Colors.
π§ Quiz
Q1. Which data structure stores multiple values?
A. Variable
B. Array
C. Function
D. Event
β Answer: B
Q2. Which function generates random numbers?
A. random()
B. generate()
C. Math.random()
D. Math.number()
β Answer: C
Q3. Which function removes decimal values?
A. round()
B. ceil()
C. floor()
D. random()
β Answer: C
Q4. Which concept is mainly used in this project?
A. Array
B. DOM Manipulation
C. Functions
D. All of These
β Answer: D
Q5. What is the purpose of the quote array?
A. Store Colors
B. Store Quotes
C. Store Images
D. Store Buttons
β Answer: B
π Lesson Summary
β Built a Random Quote Generator.
β Learned Arrays.
β Used Math.random() and Math.floor().
β Applied DOM Manipulation.
β Created an interactive JavaScript project.