Skip to content

Commit

Permalink
fix: fix null
Browse files Browse the repository at this point in the history
  • Loading branch information
FrontKid committed Aug 3, 2023
1 parent fd230ed commit ca9339f
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/arrayMethodJoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@
* Implement method join
*/

const isIgnoredEl = (el) => [null, undefined].includes(el);
const isNotIgnoredEl = (el) => ![null, undefined].includes(el);

function applyCustomJoin() {
[].__proto__.join2 = function(separator = ',') {
const INDEX_OF_LAST_ELEMENT = this.length - 1;
// if empty array return empty string
const lastElement = this[INDEX_OF_LAST_ELEMENT] || '';
const currentArray = this;
const INDEX_OF_LAST_ELEMENT = currentArray.length - 1;

let concatedString = '';

for (let curIndex = 0; curIndex < this.length; curIndex++) {
const curElement = this[curIndex];
for (let curIndex = 0; curIndex < currentArray.length; curIndex++) {
const curElement = currentArray[curIndex];

if (isIgnoredEl(curElement) && curIndex !== INDEX_OF_LAST_ELEMENT) {
concatedString += ',';
continue;
if (isNotIgnoredEl(curElement)) {
concatedString += curElement;
}

if (curIndex !== INDEX_OF_LAST_ELEMENT) {
concatedString += curElement + String(separator);
concatedString += separator;
}
}

return concatedString + lastElement;
return concatedString;
};
}

Expand Down

0 comments on commit ca9339f

Please sign in to comment.