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

Solution #2185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
48 changes: 47 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,53 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
let fromSeparator = '';

for (const arg of fromFormat) {
if (arg !== 'YYYY' || arg !== 'YY' || arg !== 'MM' || arg !== 'DD') {
fromSeparator = arg;
}
Comment on lines +13 to +16

Choose a reason for hiding this comment

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

The condition arg !== 'YYYY' || arg !== 'YY' || arg !== 'MM' || arg !== 'DD' is incorrect because it will always evaluate to true. This is because arg cannot be equal to all these values at the same time. You should use && instead of || to check if arg is not equal to any of these values.

}

const dateParts = date.split(fromSeparator);

let year = '';
let month = '';
let day = '';

for (let i = 0; i < fromFormat.length - 1; i++) {
if (fromFormat[i] === 'YYYY' || fromFormat[i] === 'YY') {
year = dateParts[i];
} else if (fromFormat[i] === 'MM') {
month = dateParts[i];
} else {
day = dateParts[i];
}
}

if (toFormat.includes('YYYY') && fromFormat.includes('YY')) {
year = year < 30 ? `20${year}` : `19${year}`;
Comment on lines +35 to +36

Choose a reason for hiding this comment

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

The logic for converting 'YY' to 'YYYY' assumes that years less than 30 belong to the 2000s and others to the 1900s. This might not be accurate for all use cases. Consider making this logic configurable or providing a more robust solution.

Comment on lines +35 to +36

Choose a reason for hiding this comment

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

The logic for converting 'YY' to 'YYYY' assumes that years less than 30 belong to the 2000s. This assumption might not be valid for all scenarios. Consider making this logic configurable or more robust to handle different cases.

Copy link
Author

Choose a reason for hiding this comment

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

This was also stated in the problem statement.
The function can change a separator, reorder the date parts of convert a year from 4 digits to 2 digits and back.

When converting from YYYY to YY just use 2 last digit (1997 -> 97).
When converting from YY to YYYY use 20YY if YY < 30 and 19YY otherwise.

} else if (toFormat.includes('YY') && fromFormat.includes('YYYY')) {
year = year.slice(-2);
}

const newDateParts = [];

let toSeparator;

for (let i = 0; i < toFormat.length; i++) {
if (toFormat[i] === 'YYYY' || toFormat[i] === 'YY') {
newDateParts.push(year);
} else if (toFormat[i] === 'MM') {
newDateParts.push(month);
} else if (toFormat[i] === 'DD') {
newDateParts.push(day);
} else {
toSeparator = toFormat[i];
Comment on lines +43 to +53

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 toSeparator assumes that the separator is not part of the format strings, which might not always be the case. Ensure that separators are correctly identified and handled, even if they are not explicitly included as the last element in the format arrays.

}
}

return newDateParts.join(toSeparator);
}

module.exports = formatDate;
Loading