-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
base: master
Are you sure you want to change the base?
Solution #2185
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was also stated in the problem statement. When converting from YYYY to YY just use 2 last digit (1997 -> 97). |
||
} 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for determining the |
||
} | ||
} | ||
|
||
return newDateParts.join(toSeparator); | ||
} | ||
|
||
module.exports = formatDate; |
There was a problem hiding this comment.
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 totrue
. This is becausearg
cannot be equal to all these values at the same time. You should use&&
instead of||
to check ifarg
is not equal to any of these values.