From c9a13edb11429a56cfe03990b7ab18453de399c9 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 26 May 2026 10:45:47 +0200 Subject: [PATCH 1/2] add task solution --- src/formatDate.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e27661..0a1017982 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -1,14 +1,57 @@ 'use strict'; /** - * @param {string} date + * @param {string} year * @param {string[]} fromFormat * @param {string[]} toFormat * * @returns {string} */ +function convertYear(year, fromFormat, toFormat) { + if ( + (fromFormat.length === 2 && toFormat.length === 2) || + (fromFormat.length === 4 && toFormat.length === 4) + ) { + return year; + } + + if (fromFormat.length === 4 && toFormat.length === 2) { + return year.slice(year.length - 2); + } + + return year < 30 ? '20'.concat(year) : '19'.concat(year); +} + function formatDate(date, fromFormat, toFormat) { // write code here + const fromSeparator = fromFormat[fromFormat.length - 1]; + const dateParts = date.split(fromSeparator); + const fromFormatParts = fromFormat.slice(0, fromFormat.length - 1); + const fromFormatObj = fromFormatParts.reduce((previous, current, index) => { + return { + ...previous, + [current]: dateParts[index], + }; + }, {}); + + const yearFromFormat = fromFormatParts.find((format) => format.includes('Y')); + + const toSeparator = toFormat[toFormat.length - 1]; + const toFormatParts = toFormat.slice(0, toFormat.length - 1); + + return toFormatParts + .reduce((prevDateParts, currentPartFormat) => { + const isYear = currentPartFormat.includes('Y'); + const year = convertYear( + fromFormatObj[yearFromFormat], + yearFromFormat, + currentPartFormat, + ); + const datePart = isYear ? year : fromFormatObj[currentPartFormat]; + + return [...prevDateParts, datePart]; + }, []) + .join(toSeparator); } module.exports = formatDate; From b9a43c3956dea6a8b81fabff96e5f37272c27d75 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 26 May 2026 10:46:59 +0200 Subject: [PATCH 2/2] add task solution --- src/formatDate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 0a1017982..6a2cd1752 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -1,7 +1,7 @@ 'use strict'; /** - * @param {string} year + * @param {string} date * @param {string[]} fromFormat * @param {string[]} toFormat *