Course Content
Project file
0/1
Complete Web Designing Course for Beginners

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

  1. What is an Array?
  2. What does Math.random() do?
  3. What does Math.floor() do?
  4. How does the Random Quote Generator work?
  5. Create a Quote Generator project.
  6. Add Quote Authors.
  7. 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.

Scroll to Top