JSFiddle - React, Tailwind, and code Playground

by Andrea Ercolino

HTML

<div id="task_description" style="height: 868px;"><div id="brinza-task-description" class="protected">
<p>You are given a year Y and the names of two months A and B. Calculate how many whole weeks are contained in the period of time between months A and B (inclusive) in year Y. A whole week consists of 7 consecutive days from Monday to Sunday (in that order). For convenience, you are also given W, the day of the week for January 1st of year Y.</p>
<p>The names of the days of the week are Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. The names of the months are January, February, March, April, May, June, July, August, September, October, November and December. The lengths of the corresponding months are 31, 28 (or 29 in a leap year), 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31 days. Assume that a year is a leap year if it is divisible by 4.</p>
<p>Write a function:</p>
<blockquote><p class="lang-js" style="font-family: monospace; font-size: 9pt; display: block; white-space: pre-wrap"><tt>function solution(Y, A, B, W);</tt></p></blockquote>
<p>that, given an integer Y and three non-empty strings A, B and W, returns the number of whole weeks in the period of time described above.</p>
<p>For example, given Y = 2014, A = "<tt style="white-space:pre-wrap">April</tt>", B = "<tt style="white-space:pre-wrap">May</tt>" and W = "<tt style="white-space:pre-wrap">Wednesday</tt>", the function should return 7, since there are seven whole weeks between April and May in the year 2014, starting respectively on April 7th, April 14th, April 21st, April 28th, May 5th, May 12th and May 19th.</p>
<p>Assume that:</p>
<blockquote><ul style="margin: 10px;padding: 0px;"><li>Y is an integer within the range [<span class="number">2,001</span>..<span class="number">2,099</span>];</li>
<li>A and B are valid month names;</li>
<li>month B does not preceed month A;</li>
<li>W is a valid name of a day of the week;</li>
<li>W is the correct day of the week for January 1st of...

JavaScript

// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

// This excercise is a bit off target, don't you think??
// Jaja, I can't even copy and paste from left to right panes here...

// write your code in JavaScript (Node.js 4.0.0)
function solution(Y, A, B, W) {
    var result = -1;
    
    var days = {
        'January':31,
        'February':28,
        'March':31,
        'April':30,
        'May':31,
        'June':30,
        'July':31,
        'August':31,
        'September':30,
        'October':31,
        'November':30,
        'December':31
    };
    
    var totalDaysBefore = daysInBetween(Y, 'January', A) - days[A];
    
    var totalDays = daysInBetween(Y, A, B);
    
    result = Math.floor(totalDays / 7);  // so this is without taking into account the start of the week...
    
    var week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    
    var startsOnMonday = (week.indexOf(W) + totalDaysBefore) % 7 === 0;
    
    if (! startsOnMonday) {
    	result -= 1;
    }
    
    return result;
    

    function daysPerMonth(Y, M) {
        if (Y % 4 === 0 && M === 'February') {
            return 29;
        }
        return days[M];
    }
    
    function daysInBetween(Y, A, B) {
        var result = 0;
        var months = Object.keys(days);
        for (i = months.indexOf(A), iTop = months.indexOf(B); i <= iTop; i++) {
            M = months[i];
            result += daysPerMonth(Y, M);
        }
        return result;
    }

}

console.log(solution(2014, 'April', 'May', 'Wednseday'));