-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 #2682
base: master
Are you sure you want to change the base?
solution #2682
Changes from 1 commit
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 |
---|---|---|
|
@@ -4,8 +4,28 @@ | |
* Implement method join | ||
*/ | ||
function applyCustomJoin() { | ||
[].__proto__.join2 = function(separator) { | ||
// write code here | ||
[].__proto__.join2 = function(separator = ',') { | ||
const currentArrayLength = this.length; | ||
const joinSeparator = separator + ''; | ||
let resultingString = ''; | ||
|
||
if (!currentArrayLength) { | ||
return resultingString; | ||
} | ||
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. Do we really need this? |
||
|
||
this.forEach((item, index) => { | ||
/* const stringCharacter = item ?? '' is the best choice, | ||
but I couldn`t disable eslint for the nullish operator */ | ||
const stringCharacter = item === null || item === undefined | ||
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. You can make it shorter using console.log(undefined == null) // true
console.log(null == null) // true
console.log(false == null) // false 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. using == is very dangerous for our karma) |
||
? '' | ||
: item; | ||
|
||
currentArrayLength - 1 === index | ||
? resultingString += stringCharacter | ||
: resultingString += stringCharacter + joinSeparator; | ||
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. I think better to use if statement here because it makes our code more readable. |
||
}); | ||
|
||
return resultingString; | ||
}; | ||
} | ||
|
||
|
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.
I propose to use a more readable string transform for it.