Final Project: Greeting Generator
Build a tiny program of your own
Congratulations on making it this far! Time to build a little program that pulls together everything you have learned.
The goal
We will make a greeting generator. Given a list of people and the current hour, it greets each person with the right message for the time of day.
The plan
- Keep a list of names in an array.
- Write a function that picks a greeting based on the hour using if / else.
- Use a loop to greet every person on the list.
- Use a template string to build a friendly sentence.
function timeGreeting(hour) {
if (hour < 12) {
return "Good morning";
} else if (hour < 18) {
return "Good afternoon";
} else {
return "Good evening";
}
}The starter code below is complete and runnable. Run it first to see it work, then make it your own:
- Add your own name to the
peoplearray. - Change
hourto a different number (try9,15, and21) and see the greetings change. - As a challenge, add a fourth time period, like a late-night message for hours after 22.
You did it
You have written real JavaScript using variables, strings, numbers, booleans, arrays, conditions, loops, and functions. That is the core toolkit that everything else builds on. Well done!
Press “Run code” to see the result.
Quick check
Q1 In the greeting generator, what decides which message timeGreeting returns?
Q2 Why does the loop use people.length as its limit?
Want to save your progress? It's free.
Create a free account