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 done #2198

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
38 changes: 37 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,43 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const [toParts] = [toFormat.slice()];
const [fromParts] = [fromFormat.slice()];
const separators = ['-', '/', ' ', '.'];
const separator = findSeparator(date, separators);
const newSeparator = findSeparator(toFormat, separators);

Choose a reason for hiding this comment

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

The findSeparator function is incorrectly used here. toFormat is an array, but findSeparator expects a string. You should determine the separator for the output format differently, possibly by using a predefined separator or by ensuring toFormat is a string.


function findSeparator(enterDate, separatorsArr) {
for (let i = 0; i < separatorsArr.length; i++) {
if (enterDate.includes(separatorsArr[i])) {
return separatorsArr[i];
}
}
}

const dateParts = date.split(separator);
const dateFrom = {};
const dateTo = [];

for (let i = 0; i < fromFormat.length; i++) {
dateFrom[fromParts[i]] = dateParts[i];
}

if (dateFrom.YYYY) {
dateFrom.YY = dateFrom.YYYY.slice(-2);
} else if (dateFrom.YY) {
if (dateFrom.YY < 30) {
dateFrom.YYYY = `20${dateFrom.YY}`;
} else {
dateFrom.YYYY = `19${dateFrom.YY}`;
}
}

for (let i = 0; i < toParts.length - 1; i++) {

Choose a reason for hiding this comment

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

The loop should iterate over the entire toParts array. Change the condition to i < toParts.length to ensure all parts of the date are included in the dateTo array.

dateTo.push(dateFrom[toParts[i]]);
}

return dateTo.join(newSeparator);
}

module.exports = formatDate;
Loading