Skip to content

Instantly share code, notes, and snippets.

@stoneboyindc
Created June 3, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoneboyindc/adb073f18f0fa2f9cf88582f6753a9fd to your computer and use it in GitHub Desktop.
Save stoneboyindc/adb073f18f0fa2f9cf88582f6753a9fd to your computer and use it in GitHub Desktop.
GetColorByDay()
const oneDay=1000*60*60*24;
const firstMondayDay = new Date("June 3, 2019");
const colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Black"];
function checkFirstMonday(day) {
if (day.getDay() != 1) {
throw "The first Monday is not set correctly.";
}
}
function getColorByDay(day) {
if (day.getDay() == 0 || day.getDay() == 6) {
throw "It is weekend. No cars are made.";
}
if (day.getTime() < firstMondayDay.getTime()) {
throw "Please input a day after " + firstMondayDay;
}
if (day.getTime() > (firstMondayDay.getTime()+30*oneDay)) {
throw "Please input a day less than 30 days from " + firstMondayDay;
}
let deltaTime = day.getTime() - firstMondayDay.getTime();
let deltaDays = Math.round(deltaTime / oneDay);
let weekNo = Math.round(deltaDays / 7);
return colors[(deltaDays - weekNo*2) % colors.length];
}
// TEST
try {
checkFirstMonday(firstMondayDay);
let day = new Date("June 3, 2019");
let color = getColorByDay(day);
console.log(color);
day = new Date("June 4, 2019");
color = getColorByDay(day);
console.log(color);
day = new Date("June 10, 2019");
color = getColorByDay(day);
console.log(color);
} catch (e) {
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment