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

done #2211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

done #2211

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
51 changes: 50 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,56 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const separator = fromFormat[fromFormat.length - 1];
const splittedDate = date.split(separator);

const result = [];

for (let i = 0; i < toFormat.length - 1; i++) {
const firstLetter = toFormat[i][0];

for (const el of fromFormat) {
if (el.includes(firstLetter)) {
const index = fromFormat.indexOf(el);

result[i] = splittedDate[index];
}
}
}

for (const el of toFormat) {
if (el.includes('Y') && el.length === 2) {
const index = toFormat.indexOf(el);

if (result[index].length === 2) {
return result.join(toFormat[toFormat.length - 1]);
}

if (result[index].length === 4) {
result[index] = result[index].slice(-2);

return result.join(toFormat[toFormat.length - 1]);
Comment on lines +32 to +39

Choose a reason for hiding this comment

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

The function returns prematurely if the year is in 'YY' format and is already two digits long. This might not be the intended behavior, as it skips formatting other parts of the date. Consider removing the return statement here and allowing the loop to continue.

Comment on lines +36 to +39

Choose a reason for hiding this comment

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

Similarly, returning here when the year is in 'YYYY' format and is already four digits long may not be correct. Ensure that all parts of the date are formatted before returning.

}
}

if (el.includes('Y') && el.length === 4) {
const index = toFormat.indexOf(el);

if (result[index].length === 2) {
if (+result[index] < 30) {
result[index] = 20 + result[index];

return result.join(toFormat[toFormat.length - 1]);
} else {
result[index] = 19 + result[index];

return result.join(toFormat[toFormat.length - 1]);
Comment on lines +46 to +54

Choose a reason for hiding this comment

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

The logic for converting two-digit years to four-digit years is correct, but returning immediately after updating the year might skip formatting other parts of the date. Consider updating the result array without returning immediately.

}
}
}
}

return result.join(toFormat[toFormat.length - 1]);
}

module.exports = formatDate;
Loading