# Rate my Friendly Date Ranges algorithm

I would like if someone could take a look at my Friendly Date Ranges algorithm and give me a feedback.
My approach was to solve one testing case at a time. Firstly I’ve checked if the years are equal, returned the corresponding values, if not, returned the corresponding values. And so on…

I know that there is a better approach and I would like to learn it
Also my if/else statements are probably hard to understand, I apologize for that.

``````function makeFriendlyDates(arr) {

//Getting the date out of the input
var startingDate = arr[0].split("-");
var endindDate = arr[1].split("-");

//Setting the initial starting date
var sDay = startingDate[2],
sMonth = startingDate[1],
sYear = startingDate[0];

//Setting the initial ending date
var eDay = endindDate[2],
eMonth = endindDate[1],
eYear = endindDate[0];

//Setting an array of the months names, added a dummy array at index 0.
var months = ["free because index 0",
"January","February","March",
"April","May","June",
"July","August","September",
"Octomber","November","December"];

//Converting the years to integers.
sYear = parseInt(sYear);
eYear = parseInt(eYear);

//Converting the date's month to literal.
sMonth = months[parseInt(sMonth)];
eMonth = months[parseInt(eMonth)];

//Converting the days.
sDay = changeDayFormat(sDay);
eDay = changeDayFormat(eDay);

//Conditions:
if(sYear === eYear){
if(sMonth === eMonth){
if(sDay === eDay){
return [sMonth + " " + sDay + ", " + sYear];
}
return [sMonth + " " + sDay, eDay];
}
else if(sMonth !== eMonth){
return [sMonth + " " + sDay + ", " + sYear, eMonth + " " + eDay];
}
} else {
if(sMonth === eMonth && parseInt(sDay) > parseInt(eDay)){
return [sMonth + " " + sDay + ", " + sYear, eMonth + " " + eDay];
}

if(months.indexOf(sMonth) > months.indexOf(eMonth) && sYear+1 == eYear) {
return [sMonth + " " + sDay, eMonth + " " + eDay];
}
return [sMonth + " " + sDay + ", " + sYear, eMonth + " " + eDay + ", " + eYear];
}

}

function changeDayFormat(day) {
day = parseInt(day);
if(day === 1){
day+="st";
} else if(day === 2){
day+="nd";
} else if(day === 3){
day+="rd";
} else {
day+="th";
}

return day;
}

makeFriendlyDates(["2016-01-01", "2016-12-31"]);
``````

Your function will return `21th, 22th, 23th, 31th`.

Tests doesn’t check this edge case, yet:

1 Like