Example output
Exercise 1: β
ββββ (1/5)
Create a program that asks the user their favorite color and displays a message with the color.
Solution:
First, we need to ask the user for their favorite color. We can use the prompt() function to do this. It shows a small box where the user can type their answer.
let favoriteColor = prompt("What is your favorite color?");
Now, let's show a message with the favorite color. We can use the alert() function to display a message to the user.
alert("Your favorite color is " + favoriteColor);
That's it! Now we have a complete program that asks for the favorite color and shows a message.
Exercise 2: β
β
βββ (2/5)
Create a program that calculates the age of a pet (for example, a dog) in human years.
Solution:
Let's start by asking the user the age of their pet in pet years. We'll use the prompt() function again.
let petAge = prompt("How old is your pet in pet years?");
Now we need to convert the pet age to human years. For this example, let's assume 1 pet year is equal to 7 human years. We'll multiply the pet age by 7.
let humanAge = petAge * 7;
Finally, let's show the result to the user. We'll use the alert() function again to display the message.
alert("Your pet is " + humanAge + " years old in human years!");
Great! Now we have a program that calculates a pet's age in human years.