🟨 JavaScript tutorial for beginners · Class 8 · Lesson 6 of 18 · Free
Mini Projects – Build Small Interactive Apps with JavaScript
⏱ About 40 minutes · ▶ run real JavaScript here — nothing to install · no sign-in
In this lesson you will
- Plan a project by writing the logic first and the page code second
- Combine variables, functions, DOM changes and events in one app
- Build and change five mini projects: scoreboard, colour changer, quotes, quiz and calculator
You now know variables (Lesson 2), functions (Lesson 3), the DOM (Lesson 4) and events (Lesson 5). Separately they are like single cricket skills — batting, bowling, fielding. Today you play a full match: five small apps that use all of them together.
How do I plan a small JavaScript project?
Good programmers split a project into two parts. The logic is the thinking: the numbers and rules. The page code shows the result and listens for clicks.
Write and test the logic first, in the console, with functions. Here is the logic of a cricket scoreboard. No page yet — just runs, wickets and one function per action.
When the logic is right, connecting buttons is easy: each button just calls one function.
▶let runs = 0;2let wickets = 0;34function addRuns(n) {5 runs = runs + n;6}7function out() {8 wickets = wickets + 1;9}10const scoreText = () => `India ${runs}/${wickets}`;11addRuns(4);12addRuns(6);13out();14addRuns(1);15console.log(scoreText());
No variables yet.
▌✏️ Change this code and run it yourself
India 11/1
💡 Try it: add another out(); and addRuns(2); and predict the score before you run.
How do I make a colour-changing background with JavaScript?
Project 1. The colours are kept in an array — a list in [ ] (Lesson 7 teaches arrays fully). colours[0] is the first colour, colours[1] the second.
Each click moves an index one step forward. The % sign gives the remainder after dividing. It makes the index jump back to 0 after the last colour, so the colours go round and round.
📄 HTML of the page (tap to see / edit)
Colour 1 of 4: gold
💡 Try it: add "lightgreen" to the array. Nothing else needs to change!
How does the % operator help JavaScript go round in a loop of choices?
Let's check the trick from Project 1 slowly. With 4 colours, the index goes 0, 1, 2, 3 — and then 4 % 4 is 0, so it starts again.
Watch index in the animation. This same trick is used for slideshows, turns in a game and traffic lights.
▶const lights = ["red", "yellow", "green"];2let index = 0;34function next() {5 index = (index + 1) % lights.length;6 console.log(index, lights[index]);7}89next();10next();11next();12next();
No variables yet.
▌✏️ Change this code and run it yourself
1 yellow2 green0 red1 yellow
How do I make a random quote generator in JavaScript?
Project 2. Math.random() gives a random decimal from 0 up to (but not including) 1. Multiply by the number of quotes and round down with Math.floor() to get a random index.
Wrapping this in a function called randomItem() keeps the page code short and readable.
📄 HTML of the page (tap to see / edit)
Quotes loaded: 3Sample: Small steps every day add up.
💡 The sample quote is random, so it differs each time you run.
How do I build a simple quiz in JavaScript?
Project 3. Each option is a button. One click handler on the box checks event.target, like the tiffin menu in Lesson 5.
A function check() compares the chosen answer with the right one and returns true or false. The page code only shows the result in green or red and disables the buttons so you cannot answer twice.
📄 HTML of the page (tap to see / edit)
false true
💡 The last line tests the logic in the console before anyone clicks.
How do I make a calculator with JavaScript?
Project 4. Two number boxes and four buttons. Each button has a data-op attribute in the HTML; JavaScript reads it with event.target.dataset.op.
calculate() is pure logic: two numbers and a sign in, an answer out. Remember Lesson 2 — input values are strings, so turn them into numbers first.
📄 HTML of the page (tap to see / edit)
16 3 Cannot divide by 0
🤔 Predict the output
Think like JavaScript: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
let runs = 10;
function addRuns(n) {
runs = runs + n;
}
addRuns(4);
addRuns(6);
console.log(runs);Question 2. What does this print?
const colours = ["red", "green", "blue"]; let index = 2; index = (index + 1) % colours.length; console.log(colours[index]);
Question 3. What does this print?
const check = (answer) => answer === "Mars";
console.log(check("mars"));🧩 Your challenge
Build a ₹ pocket-money tracker. The user types an amount and clicks "Add" or "Spend". The page shows the balance, and spending more than you have is not allowed (show "Not enough money!"). Write the logic in functions first: add(amount) and spend(amount). At the end of the script, test it with add(200); spend(50); spend(500); and log the balance. Expected console output: Balance: ₹200 Balance: ₹150 Not enough money! Balance: ₹150
Balance: ₹200 Balance: ₹150 Not enough money! Balance: ₹150
📄 HTML of the page (tap to see / edit)
⚠️ Common mistakes (and how to fix them)
❌ Wrong
const a = Number(document.querySelector("#a").value);
const b = Number(document.querySelector("#b").value);
document.querySelector("#go").addEventListener("click", () => {
document.querySelector("#out").textContent = a + b;
});
console.log("Ready");No error, but if you change the numbers the answer is still 12. The values were read once when the page loaded, not when the button was clicked.
✅ Right
document.querySelector("#go").addEventListener("click", () => {
const a = Number(document.querySelector("#a").value);
const b = Number(document.querySelector("#b").value);
document.querySelector("#out").textContent = a + b;
});
console.log("Ready");Read input values inside the click handler, so you get what the user typed just now.
❌ Wrong
document.querySelector("#plus").addEventListener("click", () => {
let count = 0;
count = count + 1;
document.querySelector("#n").textContent = count;
});
console.log("Ready");No error, but the number gets stuck at 1. Each click makes a brand-new count that starts from 0.
✅ Right
let count = 0;
document.querySelector("#plus").addEventListener("click", () => {
count = count + 1;
document.querySelector("#n").textContent = count;
});
console.log("Ready");A value that must be remembered between clicks belongs outside the handler.
❌ Wrong
const quotes = ["Work hard", "Be kind", "Keep going"]; console.log(quotes[3]);
No error, but it prints undefined. The list has 3 items at positions 0, 1 and 2 — there is no position 3.
✅ Right
const quotes = ["Work hard", "Be kind", "Keep going"]; console.log(quotes[quotes.length - 1]);
Positions start at 0, so the last item is at length - 1.
📌 Remember
- Plan first: write the logic as functions, test it in the console, then connect the page.
- Keep values that must be remembered (score, balance, index) in variables outside the handlers.
- (index + 1) % length cycles through a list and wraps back to 0.
- Math.floor(Math.random() * list.length) picks a random position in a list.
- One click handler on a box plus event.target can serve many buttons.
- Read input values inside the handler and turn them into numbers with Number().
Finished the quiz and the challenge?
Questions students ask
What are good JavaScript projects for beginners?
Start with small apps you can finish in an hour: a counter, a colour changer, a quote generator, a one-question quiz, a calculator or a pocket-money tracker. Then add one new feature at a time.
How do I start a JavaScript project on my own computer?
Make a file called index.html with your HTML, and put your code at the end inside a <script> tag (or in a separate .js file linked with <script src="app.js">). Open the file in a browser and press F12 to see the console.
Why does my project work once and then stop changing?
Usually a value is read or reset in the wrong place. Read inputs inside the handler, and keep counters outside it. Use console.log inside the handler to see what is happening.
How do I get a random number in JavaScript?
Math.random() gives a decimal from 0 up to (not including) 1. For a whole number from 0 to n-1, use Math.floor(Math.random() * n). For a dice from 1 to 6: Math.floor(Math.random() * 6) + 1.