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