Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task solution #2173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,44 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const splitSeparator = fromFormat[fromFormat.length - 1];
const splitDate = date.split(splitSeparator);
Comment on lines +11 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes that the last element of fromFormat is the separator, which may not always be the case. Consider explicitly specifying the separator or handling cases where the separator is not the last element.


const joinSeparator = toFormat[toFormat.length - 1];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, the code assumes that the last element of toFormat is the separator. Ensure that this assumption holds true for all inputs or adjust the logic to handle different cases.


// variables of index date fromFormate
const fromYYYY = fromFormat.includes('YY')
? fromFormat.indexOf('YY')
: fromFormat.indexOf('YYYY');
Comment on lines +17 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining the index of the year format (YY or YYYY) is correct, but ensure that fromFormat always contains one of these formats to avoid potential errors.

const fromMM = fromFormat.indexOf('MM');
const fromDD = fromFormat.indexOf('DD');

// variables of index date toFormate
const toYYYY = toFormat.includes('YY')
? toFormat.indexOf('YY')
: toFormat.indexOf('YYYY');
Comment on lines +24 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining the index of the year format in toFormat is correct, but similar to the previous comment, ensure that toFormat always contains one of these formats to avoid potential errors.

const toMM = toFormat.indexOf('MM');
const toDD = toFormat.indexOf('DD');

if (fromFormat[fromYYYY].length > toFormat[toYYYY].length) {
splitDate[fromYYYY] = splitDate[fromYYYY].slice(2);
}
Comment on lines +30 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block correctly handles the conversion from a four-digit year to a two-digit year. Ensure that the input year is always valid to prevent slicing errors.


if (fromFormat[fromYYYY].length < toFormat[toYYYY].length) {
if (+splitDate[fromYYYY] < 30) {
splitDate[fromYYYY] = '20' + splitDate[fromYYYY];
} else {
splitDate[fromYYYY] = '19' + splitDate[fromYYYY];
}
}
Comment on lines +34 to +40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block handles the conversion from a two-digit year to a four-digit year based on a threshold. Ensure that the threshold logic (year < 30) is appropriate for all use cases.


const result = [];

result[toYYYY] = splitDate[fromYYYY];
result[toMM] = splitDate[fromMM];
result[toDD] = splitDate[fromDD];

return result.join(joinSeparator);
}

module.exports = formatDate;
Loading