Question: (Using JavaScript language to write and node.js to run) (Problem-3.test.js file) ==> const { parseDateString } = require('./solutions'); describe('Problem 3 - parseDateString() function', function ()

(Using JavaScript language to write and node.js to run)

(Problem-3.test.js file) ==>

const { parseDateString } = require('./solutions');

describe('Problem 3 - parseDateString() function', function () {

// Checks that a date uses the given year, month, day values

function assertDate(d, year, month, day) {

return d && d.getFullYear() === year && d.getMonth() === month - 1 && d.getDate() === day;

}

test('not passing a date string throws an Error', function () {

// null

expect(() => parseDateString(null)).toThrow();

// undefined

expect(() => parseDateString()).toThrow();

// empty string

expect(() => parseDateString('')).toThrow();

});

test('passing an invalid date string format throws an Error', function () {

// 2-digit year

expect(() => parseDateString('20-03-12')).toThrow();

// 1-digit month

expect(() => parseDateString('2020-3-12')).toThrow();

// using spaces vs. dashes

expect(() => parseDateString('2020 03 12')).toThrow();

// using slashes vs dashes

expect(() => parseDateString('2020/03/12')).toThrow();

// string, but not a date string

expect(() => parseDateString('invalid string')).toThrow();

});

test('passing a valid date string results in correct date', function () {

let result = parseDateString('2021-01-01');

expect(assertDate(result, 2021, 1, 1)).toBe(true);

});

test('passing a valid date string results in correct month and day', function () {

let result = parseDateString('2021-29-01');

expect(assertDate(result, 2021, 1, 29)).toBe(true);

});

});

(Solution.js file) ==> please write your answer below:

/*******************************************************************************

* Problem 3: extract Date from date string

*

* A date string is expected to be formatted as follows:

*

* YYYY-DD-MM

*

* Meaning, Year (4 digits), Day (2 digits), Month (2 digits).

*

* January 1, 2021 would therefore be the following date string:

*

* 2021-01-01

*

* Similarly, September 29, 2021 would be:

*

* 2021-29-09

*

* Write a function, parseDateString() that accepts a date string of the format

* specified above, and returns a Date object, set to the correct day.

*

* To help developers using your function, you are asked to provide detailed error

* messages when the date string is formatted incorrectly. We will use the throw

* statement to throw an error when a particular value is not what we expect, see:

* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw

*

* For example: parseDateString('01-01-01') should fail, because the year is

* not 4 digits. Similarly, parseDateString('2021-1-01') should fail because

* the day is not 2 digits, and parseDateString('2021-01-1') should fail because

* the month is not 2 digits. Also, a totally invalid date string should also

* cause an exception to be thrown, for example parseDateString(null) or

* parseDateString("this is totally wrong").

*

* @param {string} value - a date string

* @returns {Date}

******************************************************************************/

function parseDateString(value) {

// Write your code here

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!