5 JavaScript Coding Projects
5 JavaScript Coding Projects
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
2
JavaScript code Example Random Quote Generator
Project: Random Quote Generator
Create an HTML file named index.html and set up the basic structure.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
3
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Quote Generator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Random Quote Generator</h1>
<p id="quote">Click the button to generate a random quote.</p>
<button id="generateButton">Generate Quote</button>
</div>
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
display: flex;
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
4
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
h1 {
font-size: 24px;
}
button {
display: block;
margin: 10px auto;
padding: 8px 16px;
background-color: #007bff;
color: #fff;
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
5
border: none;
border-radius: 3px;
cursor: pointer;
}
const quotes = [
"The only way to do great work is to love what you do. - Steve Jobs",
"Innovation distinguishes between a leader and a follower. - Steve Jobs",
"Don't be afraid to give up the good to go for the great. - John D. Rockefeller",
"Success is not final, failure is not fatal: It is the courage to continue that counts.
- Winston Churchill",
"The future belongs to those who believe in the beauty of their dreams. -
Eleanor Roosevelt"
];
generateButton.addEventListener("click", generateRandomQuote);
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
6
function generateRandomQuote() {
const randomIndex = Math.floor(Math.random() * quotes.length);
quoteElement.textContent = quotes[randomIndex];
}
Step 4: Testing
Open the index.html file in a web browser. You should see the "Random Quote
Generator" with a button. Clicking the button will replace the quote with a
random quote from the quotes array.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
7
"The future belongs to those who believe in the
beauty of their dreams. - Eleanor Roosevelt"
];
quotes as strings. Each quote includes both the quote itself and its
attributed author.
2. We get references to the HTML elements we'll interact with:
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
9
6. We set the textContent of the quoteElement to the randomly selected
Here's a simple JavaScript project that creates a basic to-do list application. I'll
provide you with the step-by-step description and the full code for each step.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
10
Project: Simple To-Do List App
Create an HTML file named index.html and set up the basic structure.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Simple To-Do List</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>To-Do List</h1>
<input type="text" id="taskInput"
placeholder="Add a new task">
<button id="addButton">Add</button>
<ul id="taskList"></ul>
</div>
<script src="script.js"></script>
</body>
</html>
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
11
Step 2: CSS Styling
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 300px;
}
h1 {
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
12
font-size: 24px;
text-align: center;
}
input[type="text"] {
width: 100%;
padding: 10px;
margin-top: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
button {
display: block;
margin: 10px auto;
padding: 8px 16px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
ul {
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
13
list-style: none;
padding: 0;
}
addButton.addEventListener("click", addTask);
function addTask() {
const taskText = taskInput.value.trim();
const deleteButton =
document.createElement("button");
deleteButton.textContent = "Delete";
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
14
deleteButton.addEventListener("click", () => {
taskList.removeChild(li);
});
li.appendChild(deleteButton);
taskList.appendChild(li);
taskInput.value = "";
}
}
Step 4: Testing
Open the index.html file in a web browser. You should see the simple to-do list
application with an input field and an "Add" button. Enter tasks and click the
"Add" button to add them to the list. Each task will have a "Delete" button to
remove it from the list.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
15
// Attach an event listener to the "Add" button
addButton.addEventListener("click", addTask);
with: taskInput, addButton, and taskList. These are obtained using the
getElementById method, which fetches an element by its ID attribute.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
17
2. We attach an event listener to the "Add" button (addButton). This listener is
represent the task. We set its textContent to the value of the task input.
7. We create a "Delete" button for each task using
li.appendChild(deleteButton).
10. We append the task's <li> element, including the "Delete" button, to the
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
18
By following these steps, the code creates a simple to-do list application where
you can add tasks and delete them using the "Delete" button. This is a basic
example, and you can build upon it by adding more features and functionality to
create a more robust to-do list application.
Create an HTML file named index.html and set up the basic structure.
<!DOCTYPE html>
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
19
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Digital Clock</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Digital Clock</h1>
<p id="time"></p>
</div>
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
20
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
h1 {
font-size: 24px;
}
#time {
font-size: 36px;
}
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
21
Step 3: JavaScript Logic
function updateTime() {
const now = new Date();
const hours = now.getHours().toString().padStart(2,
"0");
const minutes =
now.getMinutes().toString().padStart(2, "0");
const seconds =
now.getSeconds().toString().padStart(2, "0");
const timeString =
`${hours}:${minutes}:${seconds}`;
timeElement.textContent = timeString;
}
Step 4: Testing
Open the index.html file in a web browser. You should see a digital clock displaying
the current time, updating every second.
Congratulations! You've successfully created a simple Digital Clock using HTML,
CSS, and JavaScript. This project demonstrates how to manipulate time and
update content dynamically on a webpage.
Here's the detailed breakdown of the JavaScript code:
We start by selecting the HTML element where we'll display the time using
const timeElement = document.getElementById("time");.
We define the updateTime function which:
● Gets the current date and time using new Date().
● Extracts the hours, minutes, and seconds components and formats
them with leading zeros using .padStart() method.
● Constructs a time string in the format "HH:MM:SS".
● Updates the content of timeElement with the constructed time
string.
We use setInterval(updateTime, 1000); to call the updateTime function
every 1000 milliseconds (1 second). This ensures that the clock updates
every second.
We also call updateTime(); initially to immediately display the current time
when the page loads.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
23
Feel free to explore and expand this project further by adding features like
displaying the current date, customizing the clock's appearance, or even
implementing time zones.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
24
const timeString =
`${hours}:${minutes}:${seconds}`;
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
25
a. We create a new Date object called now to capture the current date
and time.
4. We extract the hours, minutes, and seconds components from the now
object using the getHours(), getMinutes(), and getSeconds() methods. We
use the .toString().padStart(2, "0") chain to ensure that single-digit values
are formatted with a leading zero.
5. We construct a formatted time string using template literals (backticks) with
the hours, minutes, and seconds components.
6. We update the text content of the timeElement with the newly formatted
time string using timeElement.textContent = timeString.
7. We use the setInterval(updateTime, 1000) function to call the updateTime
function every 1000 milliseconds (1 second). This ensures that the displayed
time updates dynamically every second.
8. Finally, we call updateTime() initially to immediately display the current
time when the page loads.
By following these steps, the code creates a simple digital clock that continuously
updates with the current time. This project demonstrates how to use JavaScript to
interact with date and time objects, format them, and dynamically update content
on a webpage.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
26
JavaScript Code Color Flipper Example Project: Color
Flipper
Step 1: HTML Structure
Create an HTML file named index.html and set up the basic structure.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Color Flipper</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Color Flipper</h1>
<div class="color-box" id="colorBox"></div>
<button id="flipButton">Flip Color</button>
</div>
<script src="script.js"></script>
</body>
</html>
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
27
Step 2: CSS Styling
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
28
h1 {
font-size: 24px;
margin-bottom: 10px;
}
.color-box {
width: 150px;
height: 150px;
margin: 20px auto;
border-radius: 5px;
}
button {
display: block;
margin: 10px auto;
padding: 8px 16px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
29
Step 3: JavaScript Logic
const flipButton =
document.getElementById("flipButton");
const colorBox = document.getElementById("colorBox");
flipButton.addEventListener("click", flipColor);
function flipColor() {
const randomIndex = Math.floor(Math.random() *
colors.length);
colorBox.style.backgroundColor =
colors[randomIndex];
}
Step 4: Testing
Open the index.html file in a web browser. You should see a Color Flipper with a
colored box and a "Flip Color" button.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
30
Congratulations! You've successfully created a simple Color Flipper using HTML,
CSS, and JavaScript. This project demonstrates how you can use JavaScript to
change the appearance of an element dynamically based on user interaction.
Here's the detailed breakdown of the JavaScript code:
We start by getting references to the HTML elements we'll be interacting
with: flipButton and colorBox.
We attach an event listener to the "Flip Color" button (flipButton). This
listener is set to call the flipColor function when the button is clicked.
The flipColor function is defined. This function:
● Generates a random index using Math.random() and Math.floor()
const flipButton =
document.getElementById("flipButton");
const colorBox = document.getElementById("colorBox");
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
31
In this step, we're using the document.getElementById() method to retrieve
references to the HTML elements we'll be interacting with:
● flipButton: The "Flip Color" button.
● colorBox: The colored box element where we'll change the background
color.
flipButton.addEventListener("click", flipColor);
Here, we're attaching an event listener to the "Flip Color" button. The flipColor
function will be called when the button is clicked.
function flipColor() {
const randomIndex = Math.floor(Math.random() *
colors.length);
colorBox.style.backgroundColor =
colors[randomIndex];
}
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
32
Here's a detailed breakdown of the flipColor function:
● When the "Flip Color" button is clicked, the flipColor function is executed.
selected color.
● We set the backgroundColor property of the colorBox element to the
randomly selected color. This changes the background color of the box
dynamically.
Step 4: Testing
When the user clicks the "Flip Color" button, the flipColor function is executed.
This function generates a random index, selects a color from the colors array, and
changes the background color of the colorBox element to the selected color.
This project showcases how JavaScript can be used to modify the appearance of
elements on a webpage based on user interactions. It's a simple yet effective
demonstration of how JavaScript can create dynamic and visually engaging web
experiences.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
33
JavaScript Code Tip Calculator Example Project: Tip
Calculator
Create an HTML file named index.html and set up the basic structure.
<!DOCTYPE html>
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
34
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Tip Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Tip Calculator</h1>
<label for="billAmount">Bill Amount:</label>
<input type="number" id="billAmount"
step="0.01">
<label for="serviceQuality">Service
Quality:</label>
<select id="serviceQuality">
<option value="0.2">Excellent
(20%)</option>
<option value="0.15">Good (15%)</option>
<option value="0.1">Fair (10%)</option>
<option value="0.05">Poor (5%)</option>
</select>
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
35
<button id="calculateButton">Calculate
Tip</button>
<p id="totalTip"></p>
</div>
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
36
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
h1 {
font-size: 24px;
margin-bottom: 10px;
}
label {
display: block;
margin-top: 10px;
font-weight: bold;
}
input, select {
width: 100%;
padding: 8px;
margin-top: 5px;
border: 1px solid #ccc;
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
37
border-radius: 3px;
}
button {
display: block;
margin: 10px auto;
padding: 8px 16px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
#totalTip {
font-size: 18px;
font-weight: bold;
margin-top: 10px;
}
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
38
const calculateButton =
document.getElementById("calculateButton");
const billAmountInput =
document.getElementById("billAmount");
const serviceQualityInput =
document.getElementById("serviceQuality");
const totalTipElement =
document.getElementById("totalTip");
calculateButton.addEventListener("click",
calculateTip);
function calculateTip() {
const billAmount =
parseFloat(billAmountInput.value);
const serviceQuality =
parseFloat(serviceQualityInput.value);
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
39
totalTipElement.textContent = `Tip:
$${tipAmount.toFixed(2)} | Total:
$${totalAmount.toFixed(2)}`;
} else {
totalTipElement.textContent = "Please enter
valid values.";
}
}
Step 4: Testing
Open the index.html file in a web browser. You should see a Tip Calculator with
input fields for the bill amount and service quality, as well as a button to calculate
the tip.
Congratulations! You've successfully created a simple Tip Calculator using HTML,
CSS, and JavaScript. This project showcases how you can interact with user input
and perform calculations on a webpage.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
40
We attach an event listener to the "Calculate Tip" button (calculateButton).
This listener is set to call the calculateTip function when the button is
clicked.
The calculateTip function is defined. This function:
● Retrieves the values entered by the user for the bill amount and
service quality.
● Checks if the entered values are valid numbers using isNaN().
● If the values are valid, it calculates the tip amount and the total
totalTipElement.
By following these steps, the code creates a simple Tip Calculator that calculates
the tip and total amount based on the user's input. This project demonstrates
how JavaScript can be used to perform calculations and dynamically update
content on a webpage.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
41
In this step, we're using the document.getElementById() method to retrieve
references to various HTML elements we'll be interacting with:
● calculateButton: The "Calculate Tip" button.
● billAmountInput: The input field where the user enters the bill amount.
● serviceQualityInput: The dropdown select field where the user selects the
service quality.
● totalTipElement: The paragraph element where we'll display the calculated
calculateButton.addEventListener("click",
calculateTip);
Here, we're attaching an event listener to the "Calculate Tip" button. The
calculateTip function will be called when the button is clicked.
function calculateTip() {
const billAmount =
parseFloat(billAmountInput.value);
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
42
const serviceQuality =
parseFloat(serviceQualityInput.value);
totalTipElement.textContent = `Tip:
$${tipAmount.toFixed(2)} | Total:
$${totalAmount.toFixed(2)}`;
} else {
totalTipElement.textContent = "Please enter
valid values.";
}
}
bill amount and service quality are valid numbers, we proceed with the
calculations. If not, we display an error message in the totalTipElement.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
43
● Inside the calculation block:
amounts to two decimal places, ensuring they look neat and precise.
● Finally, we update the text content of the totalTipElement with the
Step 4: Testing
When the user clicks the "Calculate Tip" button, the calculateTip function is
executed. It takes the entered bill amount and service quality, calculates the tip
and total amount, and updates the displayed result in the totalTipElement.
This project showcases how JavaScript can be used to interact with user input,
perform calculations, and dynamically update content on a webpage. It's a great
example of a practical application where JavaScript enhances user experience by
providing instant feedback on calculations.
Learn more about Google Apps Scripts with Examples and Source Code Laurence
Svekis Courses https://basescripts.com/
44