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

add solution #2195

Open
wants to merge 2 commits 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
83 changes: 82 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,88 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
let result = '';
const oldDate = date;
const newFormat = toFormat;

Choose a reason for hiding this comment

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

Directly modifying newFormat can lead to unexpected behavior if the same format is reused elsewhere. Consider working with a copy of newFormat to avoid side effects.

let array = [];
let year = '';
let yearBig = '';
const newArray = [];
let indexYear = 0;
let indexYearbig = 0;

const [, , , oldSeparator] = fromFormat;
const [, , , newSeparator] = newFormat;

array = oldDate.split(oldSeparator);

if (newFormat.includes('YYYY') && fromFormat.includes('YYYY')) {

Choose a reason for hiding this comment

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

Ensure that the alignment of formats is checked before proceeding with transformations. The assumption that 'YYYY' is present in both newFormat and fromFormat without proper alignment checks can cause errors.

for (const n of newFormat) {
const searchInd = fromFormat.indexOf(n);

newArray.push(array[searchInd]);

Choose a reason for hiding this comment

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

The newArray.pop() operation removes the last element, which might not be necessary or correct depending on the format. Ensure that this operation aligns with the intended format transformation.

}
newArray.pop();

Choose a reason for hiding this comment

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

The use of newArray.pop() to remove the last element may not be necessary or correct for the intended transformation. Verify that this operation aligns with the intended format transformation.

result = newArray.join(newSeparator);

return result;
}
Comment on lines +26 to +36

Choose a reason for hiding this comment

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

The logic here assumes that both 'YYYY' are present in newFormat and fromFormat, but it doesn't handle cases where they might not be aligned correctly. Consider checking the alignment of formats before proceeding.


if (newFormat.includes('YY')) {

Choose a reason for hiding this comment

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

The logic for handling 'YY' is not flexible and might not be suitable for all use cases. Consider making this logic more adaptable.

for (const n of array) {
if (n.length > 3) {
indexYear = array.indexOf(n);
year = n.slice(2);
}
}
array[indexYear] = year;

const newFormatYY = newFormat;
const newYY = newFormatYY.indexOf('YY');

newFormatYY[newYY] = 'YYYY';

for (const n of newFormatYY) {
const searchInd = fromFormat.indexOf(n);

newArray.push(array[searchInd]);
}
newArray.pop();

Choose a reason for hiding this comment

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

As with the previous comment, ensure that newArray.pop() is necessary and correct for the intended transformation.


result = newArray.join(newSeparator);

return result;
}

if (newFormat.includes('YYYY')) {

Choose a reason for hiding this comment

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

The handling of 'YYYY' when 'YY' is present in fromFormat is inconsistent. Ensure that the logic for transforming 'YY' to 'YYYY' is adaptable and consistent.

indexYearbig = fromFormat.indexOf('YY');

if (parseInt(array[indexYearbig]) >= 30) {
yearBig = '19' + array[indexYearbig];
}

if (parseInt(array[indexYearbig]) < 30) {
yearBig = '20' + array[indexYearbig];
}

array[indexYearbig] = yearBig;

const newFormatYYYY = newFormat;
const newY = newFormatYYYY.indexOf('YYYY');

newFormatYYYY[newY] = 'YY';

for (const n of newFormatYYYY) {
const searchInd = fromFormat.indexOf(n);

newArray.push(array[searchInd]);
}
newArray.pop();

Choose a reason for hiding this comment

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

Again, verify that newArray.pop() is appropriate for the transformation logic.


result = newArray.join(newSeparator);

return result;
}
}

module.exports = formatDate;
Loading