thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

07.07 · If, else & else if / Practice · 6 min

Fix the time-of-day greeting

The greeting says morning at night. Check which branch matches first and where its range should end.

Activity 7 of 7 in If, else & else if

All courses

JavaScript Basics / 07.07

Fix the time-of-day greeting

Not started

Use clear hour ranges in a single branching chain.

More options
Rob compares a clock with sunrise, daytime and moon cards to investigate a greeting.
Rob’s comic strip · If, else & else if

Choose a message with if and else

Work through 4 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Fix the time-of-day greeting

Use the original greeting ranges for a valid integer currentHour from 0–23: 6 up to 12 is Good Morning; 12 up to 18 is Good Afternoon; all other hours are Good Evening. Fix the conditions in this single if/else-if/else chain. Starting at 20, expect Good Evening.

Expected output
Good Evening

Keep the starting inputs. Print these lines in this order, without extra output.

Edit JavaScript, then run it. Check Challenge shows which output checks pass or need a fix. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
const currentHour = 20;

if (currentHour >= 6) {
  console.log("Good Morning");
} else if (currentHour >= 12) {
  console.log("Good Afternoon");
} else {
  console.log("Good Evening");
}

Need a nudge?

The first match stops the chain

Hour 20 is at least 6, so the original first condition takes it too early. Give morning an upper limit.

Check the boundaries

Morning ends before 12. Afternoon starts at 12 and ends before 18. Combine the start and end comparisons with &&.

Reveal a possible solution
One possible solution
example.jsjavascript · example
const currentHour = 20;

if (currentHour >= 6 && currentHour < 12) {
  console.log("Good Morning");
} else if (currentHour >= 12 && currentHour < 18) {
  console.log("Good Afternoon");
} else {
  console.log("Good Evening");
}

Check 5, 6, 11, 12, 17 and 18. Each should produce one greeting: Evening, Morning, Morning, Afternoon, Afternoon, Evening (with Good before each word). The source uses Evening for overnight hours too. This exercise assumes a valid integer hour; it does not validate dates or localize real time. The browser check compares current output only; instructor review can assess the expressions.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

A chain stops at the first matching branch. Separate if statements do not form a chain.