From 861483a77af40efc94783a63795132c66167cba8 Mon Sep 17 00:00:00 2001 From: Emil Owczarek Date: Mon, 31 Jul 2023 12:13:54 +0200 Subject: [PATCH] Solution --- src/arrayMethodJoin.js | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 17cc7949..ea2879ca 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -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;