Final Project: Greeting Generator

Build a tiny program of your own

12 min read · runnable Javascript

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

  1. Keep a list of names in an array.
  2. Write a function that picks a greeting based on the hour using if / else.
  3. Use a loop to greet every person on the list.
  4. 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 people array.
  • Change hour to a different number (try 9, 15, and 21) and see the greetings change.
  • As a challenge, add a fourth time period, like a late-night message for hours after 22.
Tip: There is no single right answer here. Experiment, break things, and fix them. That is exactly how real programmers learn.

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!

Try it yourself
Output
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