Published on

CodeWars - Unlucky Days

CodeWars - Unlucky Days


The question is basically count the number of instances of Friday the 13th in a given year.

Original Problem:

Tools needed

In order to solve this Problem you first need to be aware of the Standard Date-Object. MDN JavaScript Date

You will need to know two things

  1. How to initialize a Date const date = new Date(year, month, day)
  2. How to query the Date to get a given day. this is done with the Date.prototype.getDay() method.


To solve the problem we simply have to ask for each month of the year if it has a friday 13th. So we need a counter, and a loop for the months of the year, and then just return the value of the counter.

Code Solution

const unluckyDays = (year: number): number => {
  const FRIDAY = 5
  const UNLUCKY_DAY = 13

  let count = 0
  for (let month = 1; month < 13; month++) {
    const date = new Date(year, month, UNLUCKY_DAY)
    if (date.getDay() === FRIDAY) {
  return count

Note: it might seem that using constants for the two values FRIDAY and UNLUCKY_DAY are unnecessary. However it is good practice to avoid as many magic numbers as possible.