Question: PLEASE FOLLOW THE INSTRUCTION GIVEN. ONLY USE TRY AND CATCH WITH EASIEST WAY YOU CAN. const { toDateString, parseDateString } = require('./solutions'); // Returns true

PLEASE FOLLOW THE INSTRUCTION GIVEN. ONLY USE TRY AND CATCH WITH EASIEST WAY YOU CAN.

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

// Returns true if two dates have same year, month, day (ignoring time)

function compareDates(a, b) {

return (

a.getFullYear() === b.getFullYear() &&

a.getMonth() === b.getMonth() &&

a.getDate() === b.getDate()

);

}

describe('Problem 4 - toDateString() function', function () {

test('a date is correctly converted to a date string', function () {

let date = new Date('December 10, 2021');

let result = toDateString(date);

expect(result).toBe('2021-12-10');

});

test('a date is correctly converted to a date string with expected month and day', function () {

let date = new Date('December 17, 2021');

let result = toDateString(date);

expect(result).toBe('2021-12-17');

});

test('a date is correctly converted to a date string, with padded month', function () {

let date = new Date('January 10, 2021');

let result = toDateString(date);

expect(result).toBe('2021-01-10');

});

test('a date is correctly converted to a date string, with padded day', function () {

let date = new Date('December 01, 2021');

let result = toDateString(date);

expect(result).toBe('2021-12-01');

});

test('toDateString and parseDateString are reversible', function () {

let date = new Date('December 01, 2021');

let dateString = '2021-01-12';

expect(compareDates(parseDateString(toDateString(date)), date)).toBe(true);

expect(toDateString(parseDateString(dateString))).toBe(dateString);

});

test('an invalid date causes an error to be thrown', function () {

expect(() => toDateString('invalid date')).toThrow();

});

});

MAKE FUNCTION ACCORDING TO BELOW INSTRUCTION.

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

* Problem 4: convert Date to date string with specified format.

*

* As above, a date string is expected to be formatted as follows:

*

* YYYY-MM-DD

*

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

*

* Write a function, toDateString() that accepts a Date object, and returns a

* date string formatted according to the specification above. Make sure your

* day and month values are padded with a leading '0' if necessary (e.g., 03 vs. 3).

*

* In your solution, you are encouraged to use the following Date methods:

*

* - setFullYear()

* - setMonth()

* - setDate()

*

* NOTE: it should be possible to use parseDateString() from the previous question

* and toDateString() to reverse each other. For example:

*

* toDateString(parseDateString('2021-01-29)) should return '2021-01-29'

*

* If an invalid Date is passed, throw an Error object with an appropriate error message.

* HINT: use try/catch, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

*

* @param {Date} value - a date

* @returns {string}

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

function toDateString(value) {

// Replace this comment with your code...

}

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!