Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/arrayMethodJoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '';
Copy link

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.

Suggested change
const joinSeparator = separator + '';
const joinSeparator = String(separator);

let resultingString = '';

if (!currentArrayLength) {
return resultingString;
}

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 forEach and also return resultingString


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
Copy link

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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;
Copy link

Choose a reason for hiding this comment

The 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;
};
}

Expand Down
Loading