-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add a solution #2683
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
base: master
Are you sure you want to change the base?
Add a solution #2683
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 |
|---|---|---|
|
|
@@ -8,7 +8,35 @@ | |
| * @returns {string} | ||
| */ | ||
| function formatDate(date, fromFormat, toFormat) { | ||
| // write code here | ||
| const splitedDate = date.split(fromFormat[3]); | ||
|
|
||
| const objDate = {}; | ||
|
|
||
| for (let i = 0; i < 3; i++) { | ||
|
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 loop is hardcoded to exactly three components, which works for the provided examples, but it assumes the format will always have three date tokens. If |
||
| objDate[fromFormat[i]] = splitedDate[i]; | ||
| } | ||
|
|
||
| const result = []; | ||
|
|
||
| for (let item = 0; item < 3; item++) { | ||
|
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. Similarly, this loop assumes exactly three date-part positions in |
||
| if (objDate[toFormat[item]]) { | ||
| result.push(objDate[toFormat[item]]); | ||
| } else if (toFormat[item] === 'YY') { | ||
| const fullYear = objDate['YYYY']; | ||
|
|
||
| result.push(fullYear.slice(-2)); | ||
| } else if (toFormat[item] === 'YYYY') { | ||
| const shortYear = objDate['YY']; | ||
|
|
||
| if (shortYear >= 30) { | ||
| result.push('19' + shortYear); | ||
| } else { | ||
| result.push('20' + shortYear); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result.join(toFormat[3]); | ||
|
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. Joining with |
||
| } | ||
|
|
||
| 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.
This split logic assumes the separator is always at
fromFormat[3]. The description definesfromFormatas a format descriptor array but doesn’t guarantee that the separator is always at index 3; if that changes, this will fail. Consider finding the non-token element dynamically instead of hardcoding the index.