curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68c497f3aaefc9fd9f1b0e24.md
For day six of Space Week, you will be given a date in the format "YYYY-MM-DD" and need to determine the phase of the moon for that day using the following rules:
Use a simplified lunar cycle of 28 days, divided into four equal phases:
"New": days 1 - 7"Waxing": days 8 - 14"Full": days 15 - 21"Waning": days 22 - 28After day 28, the cycle repeats with day 1, a new moon.
"2000-01-06" as a reference new moon (day 1 of the cycle) to determine the phase of the given day.Note: Day 1 represents the day of the new moon, meaning 0 days have passed since the last new moon.
moonPhase("2000-01-12") should return "New".
assert.equal(moonPhase("2000-01-12"), "New");
moonPhase("2000-01-13") should return "Waxing".
assert.equal(moonPhase("2000-01-13"), "Waxing");
moonPhase("2014-10-15") should return "Full".
assert.equal(moonPhase("2014-10-15"), "Full");
moonPhase("2012-10-21") should return "Waning".
assert.equal(moonPhase("2012-10-21"), "Waning");
moonPhase("2022-12-14") should return "New".
assert.equal(moonPhase("2022-12-14"), "New");
function moonPhase(dateString) {
return dateString;
}
function moonPhase(dateString) {
const ONE_DAY_IN_MS = 1000 * 60 * 60 * 24;
const refDate = new Date("2000-01-06");
const targetDate = new Date(dateString);
const diffMs = targetDate - refDate;
const diffDays = diffMs / ONE_DAY_IN_MS;
const phaseDay = (diffDays % 28 + 1);
if (phaseDay <= 7) {
return "New";
} else if (phaseDay <= 14) {
return "Waxing";
} else if (phaseDay <= 21) {
return "Full"
} else {
return "Waning";
}
}