10 Days Of JavaScript

Day 6: JavaScript Dates Solution

Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 6: JavaScript Dates Solution.


Also visit this link:ย  Day 6: Bitwise Operators Solution


 

Day 6: JavaScript Dates Solution

Objective

In this challenge, we learn about JavaScriptย Dates.

Task

Given a date string,ย dateString, in the formatย MM/DD/YYYY, find and return the day name for that date. Each day name must be one of the following strings:ย Sunday,ย Monday,ย Tuesday,ย Wednesday,ย Thursday,ย Friday, orย Saturday. For example, the day name for the dateย 12/07/2016ย isย Wednesday.

Input Format

Locked stub code in the editor reads the following input from stdin:
The first line contains an integer,ย d, denoting the number of dates to check.
Each lineย iย of theย dย subsequent lines contains a date inย MM/DD/YYYYย format; each date denotes someย dataStringย that is passed to the function.

Constraints

  • It is guaranteed that the input only consists of valid dates.

Output Format

The function must return a string denoting the day of the week corresponding to the date denoted byย dateString.

Sample Input 0

2
10/11/2009
11/10/2010

Sample Output 0

Sunday
Wednesday

Explanation 0

The function is called for the followingย dย = 2ย dates:

  1. The dateย 10/11/2009ย was a Sunday, so we returnย Sunday.
  2. The dateย 11/10/2010ย was a Wednesday, so we returnย Wednesday.

 

Solution โ€“ Day 6: JavaScript Dates


'useย strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

letย inputStringย =ย '';
letย currentLineย =ย 0;

process.stdin.on('data',ย inputStdinย =>ย {
ย ย ย ย inputStringย +=ย inputStdin;
});

process.stdin.on('end',ย _ย =>ย {
ย ย ย ย inputStringย =ย inputString.trim().split('\n').map(stringย =>ย {
ย ย ย ย ย ย ย ย returnย string.trim();
ย ย ย ย });
ย ย ย ย 
ย ย ย ย main();ย ย ย ย 
});

functionย readLine()ย {
ย ย ย ย returnย inputString[currentLine++];
}

//ย Theย daysย ofย theย weekย are:ย "Sunday",ย "Monday",ย "Tuesday",ย "Wednesday",ย "Thursday",ย "Friday",ย "Saturday"
functionย getDayName(dateString)ย {
ย ย ย ย letย dayName;
ย ย ย ย //ย Writeย yourย codeย here
ย ย ย ย letย dayNamesย =ย ["Sunday",ย "Monday",ย "Tuesday",ย "Wednesday",ย "Thursday",ย "Friday",ย "Saturday"];
ย ย ย ย letย dateย =ย newย Date(dateString);
ย ย ย ย 
ย ย ย ย dayNameย =ย dayNames[date.getDay()];
ย ย ย ย 
ย ย ย ย returnย dayName;
}


functionย main()ย {
ย ย ย ย constย dย =ย +(readLine());
ย ย ย ย 
ย ย ย ย forย (letย iย =ย 0;ย iย <ย d;ย i++)ย {
ย ย ย ย ย ย ย ย constย dateย =ย readLine();
ย ย ย ย ย ย ย ย 
ย ย ย ย ย ย ย ย console.log(getDayName(date));
ย ย ย ย }
}

Complete the function in the editor. It has two parameters: a and b. It must return an object modeling a rectangle that has the following properties: Complete the function in the editor. It has two parameters:ย aย andย b. It must return an object modeling a rectangle that has the following properties: