From 1681683cce8c9c0360628ee7a9dd40ea3963b118 Mon Sep 17 00:00:00 2001 From: myName Date: Tue, 8 Aug 2023 10:34:47 +0200 Subject: [PATCH 1/5] solution --- src/arrayMethodJoin.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 3a62201c1..7b79677ca 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -4,8 +4,31 @@ * Implement method join */ function applyCustomJoin() { - [].__proto__.join2 = function(separator) { - // write code here + [].__proto__.join2 = function(separator = ',') { + const arrCopy = [ ...this ]; + let ConcatenatedStr = ''; + + for (let i = 0; i < arrCopy.length; i++) { + if (arrCopy[i] === undefined || arrCopy[i] === null) { + arrCopy[i] = ''; + } + } + + if (arrCopy.length > 0) { + for (let i = 0; i < arrCopy.length - 1; i++) { + arrCopy[i] += separator; + + if (separator === null) { + arrCopy[i] += 'null'; + } + + ConcatenatedStr += arrCopy[i]; + } + + ConcatenatedStr += arrCopy[arrCopy.length - 1]; + } + + return ConcatenatedStr; }; } From ad217467885ba7d0e0ae29f13512577060cdd9c7 Mon Sep 17 00:00:00 2001 From: myName Date: Tue, 8 Aug 2023 10:37:43 +0200 Subject: [PATCH 2/5] task solution --- src/arrayMethodJoin.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 7b79677ca..f9b4cf376 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -8,26 +8,28 @@ function applyCustomJoin() { const arrCopy = [ ...this ]; let ConcatenatedStr = ''; + if (arrCopy.length === 0) { + return ''; + } + for (let i = 0; i < arrCopy.length; i++) { if (arrCopy[i] === undefined || arrCopy[i] === null) { arrCopy[i] = ''; } } - if (arrCopy.length > 0) { - for (let i = 0; i < arrCopy.length - 1; i++) { - arrCopy[i] += separator; + for (let i = 0; i < arrCopy.length - 1; i++) { + arrCopy[i] += separator; - if (separator === null) { - arrCopy[i] += 'null'; - } - - ConcatenatedStr += arrCopy[i]; + if (separator === null) { + arrCopy[i] += 'null'; } - ConcatenatedStr += arrCopy[arrCopy.length - 1]; + ConcatenatedStr += arrCopy[i]; } + ConcatenatedStr += arrCopy[arrCopy.length - 1]; + return ConcatenatedStr; }; } From 86af661f3ad24680d8f426bb94f7b54cce222c93 Mon Sep 17 00:00:00 2001 From: myName Date: Wed, 9 Aug 2023 16:33:24 +0200 Subject: [PATCH 3/5] fix --- src/arrayMethodJoin.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index f9b4cf376..5705e9966 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -6,9 +6,9 @@ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { const arrCopy = [ ...this ]; - let ConcatenatedStr = ''; + let concatenatedStr = ''; - if (arrCopy.length === 0) { + if (!arrCopy.length) { return ''; } @@ -25,12 +25,12 @@ function applyCustomJoin() { arrCopy[i] += 'null'; } - ConcatenatedStr += arrCopy[i]; + concatenatedStr += arrCopy[i]; } - ConcatenatedStr += arrCopy[arrCopy.length - 1]; + concatenatedStr += arrCopy[arrCopy.length - 1]; - return ConcatenatedStr; + return concatenatedStr; }; } From cc7b1fa6b52ad468d22051995557dd66f133f95a Mon Sep 17 00:00:00 2001 From: myName Date: Thu, 10 Aug 2023 10:32:03 +0200 Subject: [PATCH 4/5] edits --- src/arrayMethodJoin.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 5705e9966..e1b830aa6 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -5,32 +5,29 @@ */ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { - const arrCopy = [ ...this ]; - let concatenatedStr = ''; + let result = ''; - if (!arrCopy.length) { - return ''; - } + for (let i = 0; i < this.length - 1; i++) { + let currentElement = this[i]; - for (let i = 0; i < arrCopy.length; i++) { - if (arrCopy[i] === undefined || arrCopy[i] === null) { - arrCopy[i] = ''; + if (currentElement === null || currentElement === undefined) { + currentElement = ''; } - } - for (let i = 0; i < arrCopy.length - 1; i++) { - arrCopy[i] += separator; + result += currentElement; - if (separator === null) { - arrCopy[i] += 'null'; - } + result += separator; + } - concatenatedStr += arrCopy[i]; + if (this.length === 1) { + result += this[0]; } - concatenatedStr += arrCopy[arrCopy.length - 1]; + if (this[this[this.length - 1]]) { + result += this[this.length - 1]; + } - return concatenatedStr; + return result; }; } From 916424e13e775906e5cfaa3b38f5b28a879a063c Mon Sep 17 00:00:00 2001 From: myName Date: Thu, 10 Aug 2023 10:35:37 +0200 Subject: [PATCH 5/5] add task solution --- src/arrayMethodJoin.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index e1b830aa6..8519500b2 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -19,11 +19,7 @@ function applyCustomJoin() { result += separator; } - if (this.length === 1) { - result += this[0]; - } - - if (this[this[this.length - 1]]) { + if (this[this.length - 1]) { result += this[this.length - 1]; }