JavaScript: Range of dates

Last update: 12 Feb 23:20
0
Students

dates.js

Implement and export as default a function that converts input data into a format suitable for plotting.

This function takes three arguments: a data array, the period's start date, and the period's end date. The data is given as an object of the form {value: 14, date: '08/02/2018' }, and the dates format is 'yyyy-MM-dd'.

The date range specifies the output array size that the implemented function should produce. Rules for forming the output array:

  • it is filled with entries for each day in the begin - end range
  • if there is no day data in the input array, then set the value 0 in the value property of a day entry
import buildRange from './dates.js';

const dates = [
  { value: 14, date: '02.08.2018' },
  { value: 43, date: '03.08.2018' },
];
const beginDate = '2018-08-01';
const endDate = '2018-08-04';

buildRange(dates, beginDate, endDate);
// [
//   { value: 0, date: '01.08.2018' },
//   { value: 14, date: '02.08.2018' },
//   { value: 43, date: '03.08.2018' },
//   { value: 0, date: '04.08.2018' },
// ]

Tips

Documentation on functions for working with dates:

  • https://date-fns.org/v2.16.1/docs/eachDayOfInterval
  • https://date-fns.org/v2.16.1/docs/format

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests