Created
May 31, 2021 11:15
-
-
Save oscard0m/484b3c6911369b83f531b960c33d229f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
function isLeapYear(year) { | |
return year % 4 === 0 | |
} | |
const months = [ | |
'January', | |
'February', | |
'March', | |
'April', | |
'May', | |
'June', | |
'July', | |
'August', | |
'September', | |
'October', | |
'November', | |
'December', | |
] | |
const monthDays = [ | |
31, | |
28, | |
31, | |
30, | |
31, | |
30, | |
31, | |
31, | |
30, | |
31, | |
30, | |
31, | |
] | |
const weekdays = [ | |
'Monday', | |
'Tuesday', | |
'Wednesday', | |
'Thursday', | |
'Friday', | |
'Saturday', | |
'Sunday' | |
] | |
function getWeekDayFirstOfMonth(monthIndex, firstWeekDayOfTheYear, year) { | |
let monthFound = false; | |
// let dayOfTheWeekIndex = | |
let accumDays = weekdays.indexOf(firstWeekDayOfTheYear) | |
for(i = 0; !monthFound && i < months.length; i++) { | |
monthFound = i === monthIndex | |
if(!monthFound) { | |
accumDays += monthDays[i] | |
} | |
} | |
//TODO isLeapYear | |
if(isLeapYear(year) && monthIndex > 1) { | |
accumDays++ | |
} | |
return weekdays[accumDays % 7] | |
} | |
function solution(year, startingMonth, endMonth, firstWeekDayOfTheYear) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
let daysOfVacations = 0; | |
const startingMonthIndex = months.indexOf(startingMonth) | |
const endMonthIndex = months.indexOf(endMonth) | |
const firstDayOfVacationsMonth = getWeekDayFirstOfMonth(startingMonthIndex, firstWeekDayOfTheYear, year) | |
const firstDayOfWorkMonth = getWeekDayFirstOfMonth(endMonthIndex + 1, firstWeekDayOfTheYear) | |
console.log(firstDayOfVacationsMonth) | |
console.log(firstDayOfWorkMonth) | |
for(let i = startingMonthIndex; i <= endMonthIndex; i++) { | |
daysOfVacations += monthDays[i] | |
} | |
if(firstDayOfVacationsMonth !== 'Monday') { | |
daysOfVacations -= 7 - weekdays.indexOf(firstDayOfVacationsMonth) + 1 | |
} | |
if(firstDayOfWorkMonth !== 'Monday') { | |
daysOfVacations -= 7 - (7 - weekdays.indexOf(firstDayOfWorkMonth) + 1) | |
} | |
return daysOfVacations / 7 | |
} | |
let year = 2014 | |
let startingMonth = 'April' | |
let endMonth = 'May' | |
let firstWeekDayOfTheYear = 'Wednesday' | |
console.log(solution(year, startingMonth, endMonth, firstWeekDayOfTheYear)) //7 | |
year = 2027 | |
startingMonth = 'April' | |
endMonth = 'May' | |
firstWeekDayOfTheYear = 'Friday' | |
console.log(solution(year, startingMonth, endMonth, firstWeekDayOfTheYear)) //7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment