-
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
Method join #2651
base: master
Are you sure you want to change the base?
Method join #2651
Conversation
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.
Needs some refactoring
src/arrayMethodJoin.js
Outdated
@@ -5,7 +5,34 @@ | |||
*/ | |||
function applyCustomJoin() { | |||
[].__proto__.join2 = function(separator) { | |||
// write code here | |||
let separatorValue = separator; |
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.
You may use the separator
parameter and get rid of this declaration along with if, if you add a default value in the parameter declaration
[].__proto__.join2 = function(separator = ',') {
src/arrayMethodJoin.js
Outdated
separatorValue = ','; | ||
} | ||
|
||
const arr = this; |
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'd say arr
variable is unnecessary, from the context of this implementation it's implied this refers to an array
src/arrayMethodJoin.js
Outdated
return ''; | ||
} | ||
|
||
for (const element of arr) { |
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.
If you need an index regular for loop with let i = 0; i < this.length; i++
would be better
src/arrayMethodJoin.js
Outdated
|
||
for (const element of arr) { | ||
const str = element === null | ||
|| element === undefined ? '' : element.toString(); |
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.
element.toString();
-> regarding this part, I personally am not sure whether all data types have .toString
method, it would be safer to user something like String(element)
or interpolate ${element}
result += this[i] === null || this[i] === undefined ? '' : String(this[i])
src/arrayMethodJoin.js
Outdated
|
||
result += str; | ||
|
||
if (arr.indexOf(element) < arr.length - 1) { |
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.
If you go with the previously suggested changes this here could be shortened to:
if (i < this.length - 1) {
result += String(separator)
}
I refactor this code like you said and yea is shorter and more readable :D |
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.
Good job one suggestion from my side, for more readability, you can apply it
Approving 💪
src/arrayMethodJoin.js
Outdated
result += this[i] === null || this[i] === undefined | ||
? '' : String(this[i]); |
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.
In my opinion this would be even more readable:
if (this[i] !== undefined && this[i] !== null) {
result += String(this[i]);
}
Soultion to tests