From 56c43071eb50e21479c8ffb4cb924dd7769f9096 Mon Sep 17 00:00:00 2001 From: Taras Voievoda Date: Wed, 2 Aug 2023 17:13:15 +0300 Subject: [PATCH 1/3] add task solution --- src/arrayMethodJoin.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 3a62201c1..2d49152fc 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -4,8 +4,20 @@ * Implement method join */ function applyCustomJoin() { - [].__proto__.join2 = function(separator) { - // write code here + [].__proto__.join2 = function(separator = ',') { + let str = ''; + + for (let i = 0; i < this.length; i++) { + if (this[i] !== null && this[i] !== undefined) { + str += this[i]; + } + + if (i !== this.length - 1) { + str += separator; + } + } + + return str; }; } From 9c8301e1cab2cc34cd3576905ed207ada734911f Mon Sep 17 00:00:00 2001 From: Taras Voievoda Date: Mon, 7 Aug 2023 21:13:59 +0300 Subject: [PATCH 2/3] create variable --- src/arrayMethodJoin.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 2d49152fc..04f4200b4 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -5,19 +5,21 @@ */ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { - let str = ''; + const STRING_SEPARATOR = String(separator); + let finalString = ''; for (let i = 0; i < this.length; i++) { - if (this[i] !== null && this[i] !== undefined) { - str += this[i]; - } + const arrayElement = this[i]; - if (i !== this.length - 1) { - str += separator; + if (arrayElement === undefined || arrayElement === null) { + finalString += STRING_SEPARATOR; + continue; } + + finalString += arrayElement + STRING_SEPARATOR; } - return str; + return finalString.slice(0, finalString.length - STRING_SEPARATOR.length); }; } From 38e919375f96374f24f5fc88c1839b2f157fbaa0 Mon Sep 17 00:00:00 2001 From: Taras Voievoda Date: Mon, 7 Aug 2023 21:25:51 +0300 Subject: [PATCH 3/3] previous solution --- src/arrayMethodJoin.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 04f4200b4..5f5158c4c 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -5,21 +5,21 @@ */ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { - const STRING_SEPARATOR = String(separator); - let finalString = ''; + let finalResult = ''; for (let i = 0; i < this.length; i++) { const arrayElement = this[i]; - if (arrayElement === undefined || arrayElement === null) { - finalString += STRING_SEPARATOR; - continue; + if (arrayElement !== null && arrayElement !== undefined) { + finalResult += arrayElement; } - finalString += arrayElement + STRING_SEPARATOR; + if (i !== this.length - 1) { + finalResult += separator; + } } - return finalString.slice(0, finalString.length - STRING_SEPARATOR.length); + return finalResult; }; }