Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
emil-owczarek committed Jul 31, 2023
1 parent 8ec573c commit 861483a
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions src/arrayMethodJoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,21 @@
* Implement method join
*/
function applyCustomJoin() {
[].__proto__.join2 = function(separator) {
[].__proto__.join2 = function(separator = ',') {
let output = '';

let actualSeparator;

if (separator === undefined) {
actualSeparator = ',';
} else {
actualSeparator = separator;
}

if (this.length === 0) {
return output;
}

if (this[0] !== undefined && this[0] !== null) {
output += `${this[0]}`;
} else {
output += '';
}
for (let i = 0; i < this.length; i++) {
if (i > 0) {
output += separator;
}

for (let i = 1; i < this.length; i++) {
if (this[i] !== undefined && this[i] !== null) {
output += `${actualSeparator}${this[i]}`;
} else {
output += actualSeparator;
output += this[i];
}
}

return output;
};
};
}

module.exports = applyCustomJoin;

0 comments on commit 861483a

Please sign in to comment.