Name the day of the week with switch

“Day of the week”, a White belt challenge from the CurlyScript Dojo. It uses what Grade 4: Making Decisions: if, else & Friends teaches, and 5 tests check your answer.

The challenge

Write dayName(n) where 1 is "Monday" and 7 is "Sunday". Return "Invalid day" for anything else. Try using switch!

Try it

Write your answer below, then press Check my answer. Your code runs right here in your browser, and the tests tell you what works.

What the tests check

  • dayName(1) gives "Monday"
  • dayName(5) gives "Friday"
  • dayName(7) gives "Sunday"
  • dayName(0) gives "Invalid day"
  • dayName(8) gives "Invalid day"

Hints

Stuck? Open one hint at a time. Try again after each one.

Hint 1

switch (n) { case 1: return "Monday"; … default: return "Invalid day"; }

Hint 2

Because return leaves the function, you don’t need break after each case.

A solution

There’s always more than one right way. Read it, make sure you understand it, then try writing it yourself without looking.

Show one possible solution
Solution
function dayName(n) {
  switch (n) {
    case 1: return "Monday";
    case 2: return "Tuesday";
    case 3: return "Wednesday";
    case 4: return "Thursday";
    case 5: return "Friday";
    case 6: return "Saturday";
    case 7: return "Sunday";
    default: return "Invalid day";
  }
}

Learn what you need

This exercise uses what Grade 4: Making Decisions: if, else & Friends teaches. Read those notes first if any of it is new.

Practice makes it stick

CurlyScript is a free game that teaches JavaScript from zero: short lessons, an explanation after every answer, and Dojo challenges like this one.

Start learning