Daily Coding Challenge - Space Week Day 6: Moon Phase

Tell us what’s happening:

Daily Challenge October 9, 2025 - Moon Phase

Why is the switch statement not working here, neither with the ranges from the if statement nor with the adjusted ranges?

Your code so far

function moonPhase(dateString) {
  const simpleDate = dateString.split('-');
    //console.log(simpleDate)
  
  const mil = new Date(simpleDate).getTime();
    //console.log(mil)

  const anchor = "2000-01-06".split('-');
    //console.log(date)
  const refMil = new Date(anchor).getTime();
    //console.log(refMil)

  const mil1Day = ((1000 * 60 * 60) * 24);
    //console.log(mil1Day)

  const timeDiff = (((mil - refMil) / mil1Day) % 28)+1;
    //console.log(timeDiff)
  let result = '';
  
   
     if (timeDiff >= 1 && timeDiff <= 7){
        result = 'New';
      }
      else if (timeDiff >= 8 && timeDiff <= 14) {
        result = 'Waxing'  
      }
      else if (timeDiff >= 15 && timeDiff <= 21) {
        result = 'Full'  
      }
      else if (timeDiff >= 22 && timeDiff <= 28) {
        result = 'Waning'  
      }
      else {
        console.log("The date is out of range");
        return;
      }
    
    /*
      switch (timeDiff) {
        case (timeDiff > 0 && timeDiff < 8):
        result = 'New'
        break;
        case (timeDiff >= 8 && timeDiff < 15):
        result = 'Waxing'
        break;
        case (timeDiff >= 15 && timeDiff < 22):
        result = 'Full'
        break;
        case (timeDiff >= 22 && timeDiff < 29):
        result = 'Waning'
        break;
        default:
        break;
      }
    */
    //console.log(result)
  return result
}

moonPhase("2000-01-12") //"New"
moonPhase("2000-01-13") //"Waxing"
moonPhase("2014-10-15") //"Full"
moonPhase("2012-10-21") //"Waning"
moonPhase("2022-12-14") //"New"
moonPhase("2000-01-01")
moonPhase("2104-01-01")

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0

Challenge Information:

Daily Coding Challenge - Space Week Day 6: Moon Phase

https://www.freecodecamp.org/learn/daily-coding-challenge/2025-10-09

That’s not quite how a switch case works in JavaScript. You first state an expression then provide a series of clauses which have values which may match the evaluation of that expression.

However, if you change the initial expression to true (i.e. switch (true)) then it should work.

MORE INFO:

Changing the expression to true let the challenge pass with the switch statement as well.

Agreed, the switch statement would not be the best choice in this scenario nor would its capabilities been fully made use of. The if statement is the leaner approach here, but I am a beginner and I was just playing around with it since I haven’t used it that often.

Yet, I was under the impression that with timeDiff as expression I set it to true already, as it returned an integer and the value wasn’t null, undefined or NaN, etc.
The link you’ve provided shows a similar example for an integer, however the clauses weren’t defined as ranges/in brackets and the clauses did match the expression exactly.

I suppose that was the reason for it to fail.

Thanks

The expression switch(timeDiff) will return the value of timeDiff, whilst the clauses (which are a comparison) will return a Boolean (i.e. true or false). Thus none of the clauses will ever match the value returned by switch(timeDiff).