Lesson 5: Your First Interactive Program
From static to interactive experiences
Websites become truly useful when they respond to people!
Today: We'll use prompt()
let name = prompt("What is your name?");
Shows a box asking the user for input. Stores the answer in a variable.
alert("Hello, " + name + "!");
Shows a message box to the user.
if (age >= 18) { alert("You are an adult!"); } else { alert("You are a minor!"); }
Decide what happens based on user input.
let birthYear = prompt("What year were you born?");
Ask the user for their birth year.
let currentYear = new Date().getFullYear(); let age = currentYear - birthYear;
Calculate the user's age.
alert("You are " + age + " years old.");
Show the result to the user!
let answer = prompt("What is 2 + 2?"); if (answer == 4) { alert("Correct!"); } else { alert("Try again!"); }
Respond to the user's answer.
Write a program that asks for your favorite color and responds with a message.
let color = prompt("What's your favorite color?"); alert("Nice choice! " + color);
let for variables= instead of == in comparisonsIdeas:
Show your code to a friend!
prompt() to get inputalert() to show messagesWrite a program that asks for your birth year and calculates your age in 2100.