-
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
initial commit #2678
base: master
Are you sure you want to change the base?
initial commit #2678
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.
Almost done:smiley:
src/arrayMethodJoin.js
Outdated
// write code here | ||
[].__proto__.join2 = function(separator = ',') { | ||
let stringAfterJoin = ''; | ||
const copyArray = this.slice(); |
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.
- [CODE STYLE] - avoid creating needless copies of arrays, you can iterate existing array
src/arrayMethodJoin.js
Outdated
if (finallySeparator === null) { | ||
finallySeparator = 'null'; | ||
} |
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.
little advice for you
console.log(String(null)) // 'null'
console.log(typeof String(null)) // string
const test = null
console.log(`${test}`) // 'null'
console.log("result: " + test) // result: null
src/arrayMethodJoin.js
Outdated
} | ||
|
||
for (let i = 0; i < copyArray.length; i++) { | ||
if (copyArray[i] === undefined || copyArray[i] === null) { |
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.
try to invert this condition i think if we go in this direction it might help us to write shorter code
if (copyArray[i] === undefined || copyArray[i] === null) { | |
if (copyArray[i] != null) { |
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.
Let's make your code cleaner
src/arrayMethodJoin.js
Outdated
// write code here | ||
[].__proto__.join2 = function(separator = ',') { | ||
let stringAfterJoin = ''; | ||
const finallySeparator = 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.
Why do we need this?
src/arrayMethodJoin.js
Outdated
if (this.length === 0) { | ||
return ''; | ||
} |
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.
Do we really need this?
If length === 0 we will skip the loop and return stringAfterJoin
which is equal to ''
src/arrayMethodJoin.js
Outdated
} | ||
|
||
for (let i = 0; i < this.length; i++) { | ||
if (this[i] !== null && this[i] !== undefined) { |
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 are using this[i]
3 times. Create variable for this value
src/arrayMethodJoin.js
Outdated
stringAfterJoin += this[i]; | ||
} | ||
|
||
if (i < this.length - 1 && finallySeparator !== undefined) { |
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 set a default value for the separator, so it will never be equal to undefined
stringAfterJoin += element; | ||
} | ||
|
||
if (i < this.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.
I suggest putting this.length in a variable, since we already use it twice, but I guess it's optional
No description provided.