Day Of Week
Description
Whenever you decide to celebrate your birthday you always do this your favorite café, which is quite popular and as such usually very crowded. This year you got lucky: when you and your friend enter the café you’re surprised to see that it’s almost empty. The waiter lets slip that there are always very few people on this day of the week.
You enjoyed having the café all to yourself, and are now curious about the next time you’ll be this lucky. Given the current birthdayDate
, determine the number of years until it will fall on the same day of the week.
For your convenience, here is the list of months lengths (from January to December, respectively):
- Months lengths:
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
.
Please, note that in leap years February has 29
days. If your birthday is on the 29th
of February, you celebrate it once in four years. Otherwise you birthday is celebrated each year.
Note (leap years): Year is leap if its number is divisible by 4 and isn’t divisible by 100 or if its number is divisible by 400.
Example
For birthdayDate = "02-01-2016"
, the output should be
dayOfWeek(birthdayDate) = 5
.
February 1 in 2016
is a Monday. The next year in which this same date will be Monday too is 2021
. 2021 - 2016 = 5
, which is the answer.
Input/Output
- [execution time limit] 4 seconds (js)
-
[input] string birthdayDate
A string representing the correct date in the format
mm-dd-yyyy
, wheremm
is the number of month (1-based, i.e.01
for January,02
for February and so on), dd is the day, andyyyy
is the year.Guaranteed constraints:
1 ≤ int(mm) ≤ 12
,
1 ≤ int(dd) ≤ 31
,
1900 ≤ int(yyyy) ≤ 2100
. -
[output] integer
- An integer describing the number of years until your birthday falls on the same day of the week.
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function dayOfWeek(birthdayDate) {
var date = new Date();
date.setTime(Date.parse(birthdayDate));
var dow = date.getDay();
var i = 1;
birthdayDate = birthdayDate.replace(/(\d{4})$/, (_, y) => Number(y) + 1);
date.setTime(Date.parse(birthdayDate));
while (
Number(birthdayDate.split("-")[0]) !== date.getMonth() + 1 ||
date.getDay() !== dow
) {
i++;
birthdayDate = birthdayDate.replace(/(\d{4})$/, (_, y) => Number(y) + 1);
date.setTime(Date.parse(birthdayDate));
}
return i;
}