You've completed the lesson!
You now know how to build interactive web applications with JavaScript!
In this interactive lesson, you'll write actual code step-by-step to create a color palette generator! You'll type code directly on this page, run it, and see instant results. By the end, you'll have built a working app that creates random colors at the click of a button.
How it works: Complete each exercise below by writing code in the interactive editors. Click "Run Code" to test it, and "Check Answer" to verify you got it right!
Interactive programming means writing code that responds to user actions. When you click a button on a website, tap on your phone, or hover over an image, code is running in the background to make something happen.
Think about the difference:
Examples you use every day:
JavaScript is what makes the web feel alive and responsive!
Here's the key difference:
Real-world analogy:
Think of a TV remote control:
Here's what happens when you visit an interactive website:
Cool fact: Your browser has a built-in JavaScript engine that's incredibly fast - it can execute millions of instructions per second!
In this lesson, you'll learn three fundamental concepts:
Containers that store information (like color codes)
Reusable blocks of code that perform specific tasks
Code that waits for user actions (like button clicks)
What is a variable?
A variable is like a labeled box where you can store information. You can put something in, take it out, or replace it with something else!
Real-world analogy: Think of variables like post-it notes
JavaScript variable example:
const myColor = "#FF5733";
const userName = "Alex";
const score = 100;
Breaking it down:
const = "Create a constant variable" (won't change)myColor = The variable name (the label on the box)= = "Store this value in the variable""#FF5733" = The value being stored (a color code)What is a function?
A function is like a recipe - a set of instructions that you can use over and over again whenever you need it!
Real-world analogy: Making a sandwich
JavaScript function example:
function sayHello() {
console.log("Hello, world!");
console.log("How are you?");
}
// Use the function:
sayHello(); // Runs all the code inside!
Why use functions?
What is an event listener?
An event listener is code that "listens" and waits for something to happen (like a click or keypress), then runs code when it does!
Real-world analogy: A doorbell
JavaScript event listener example:
button.addEventListener('click', function() {
// This code runs when the button is clicked!
alert("You clicked me!");
});
Common events you can listen for:
'click' - When user clicks something'mouseover' - When mouse hovers over something'keypress' - When user presses a key'submit' - When a form is submittedThe Hexadecimal Color System
Computers represent colors using numbers. The most common format is called "hex color codes".
Format: #RRGGBB
# = "This is a color code"RR = Amount of Red (00 to FF)GG = Amount of Green (00 to FF)BB = Amount of Blue (00 to FF)Examples:
#FF0000 = Pure red (max red, no green, no blue)#00FF00 = Pure green#0000FF = Pure blue#FFFFFF = White (maximum of all colors)#000000 = Black (no color)#FF5733 = Orange-ish (lots of red, some green, little blue)Why "00 to FF"?
It's hexadecimal (base-16) counting:
So each color channel can have 256 different levels (0-255)!
How we'll make random colors:
What does Math.random() do?
It generates a random decimal number between 0 (inclusive) and 1 (exclusive).
Examples of what it might return:
0.1234560.7890120.4567890.999999But wait - we need whole numbers from 0 to 255!
Here's how to convert it:
// Step 1: Get random decimal between 0 and 1
const random = Math.random();
// Step 2: Multiply by 256 to get 0 to 255.999...
const scaled = random * 256;
// Step 3: Round down to whole number
const final = Math.floor(scaled);
// All in one line:
const number = Math.floor(Math.random() * 256);
Why does this work?
Math.random() gives you 0.000... to 0.999...Math.floor() rounds DOWN: gives you 0 to 255!Step by step, you'll create a fully-functional color generator app!
Exercise 1: Learn to generate random numbers (the foundation)
Exercise 2: Create a function that generates random hex color codes
Exercise 3: Use your function to change colors on the page
Exercise 4: Make a button that users can click to generate colors
Exercise 5: Put it all together into a complete, polished application!
💡 Pro tip: Each exercise builds on the previous one. Master each step before moving on, and you'll have a working app by the end!
Reusable blocks of code that perform specific tasks
Actions that trigger your code (like clicking a button)
Think of your code like a pizza oven:
When you click the button in our color generator, you're "pressing start" on a function that "bakes" a new random color!
Complete each exercise below to build your color generator step by step. Write code, run it, and see instant feedback!
Goal: Create a variable that stores a random number between 0 and 255.
JavaScript's Math.random() gives a decimal between 0 and 1. To get whole numbers from 0-255, use Math.floor(Math.random() * 256).
Math.floor(Math.random() * 256) to get a whole number from 0 to 255.
Goal: Write a function that generates three random values (red, green, blue) and combines them into a hex color.
A hex color looks like #A3F5B2. You'll need to convert numbers to hexadecimal using .toString(16).
g and b variables.
Goal: Use JavaScript to change this preview box's background color to a random color.
Use document.getElementById('preview3').style.backgroundColor = colorCode; to change colors.
newColor
Goal: Add an event listener so the button below changes colors when clicked!
Event listeners watch for actions (like clicks) and run code when they happen.
Color: Not set yet
Final Challenge: Combine everything you've learned to create a full color generator!
This is your complete app! Here's what each element does:
Your task: Make the "Generate Color" button work by filling in the missing code. When someone clicks it, it should:
generateRandomColor() functiongenerateRandomColor() and store it in the newColor variablenewColor (just like Exercise 3!)newColorAnswer template:
const newColor = generateRandomColor();
previewBox.style.backgroundColor = newColor;
display.textContent = newColor;
Want to see a polished version? Check out the complete demo with extra styling!
🚀 Launch Full DemoFinished all exercises? Try these advanced modifications in the demo:
Check the README.md file for detailed hints and solutions!