From 9788cb38603e49674623b1ed19e6a175792eb774 Mon Sep 17 00:00:00 2001 From: AndrMar1939 Date: Fri, 4 Aug 2023 16:17:05 +0300 Subject: [PATCH 1/5] solution --- src/arrayMethodJoin.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 3a62201c1..e97a20045 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -4,8 +4,28 @@ * Implement method join */ function applyCustomJoin() { - [].__proto__.join2 = function(separator) { - // write code here + [].__proto__.join2 = function(separator = ',') { + const currentArrayLength = this.length; + const joinSeparator = separator + ''; + let resultingString = ''; + + if (!currentArrayLength) { + return resultingString; + } + + this.forEach((item, index) => { + /* const stringCharacter = item ?? '' is the best choice, + but I couldn`t disable eslint for the nullish operator */ + const stringCharacter = item === null || item === undefined + ? '' + : item; + + currentArrayLength - 1 === index + ? resultingString += stringCharacter + : resultingString += stringCharacter + joinSeparator; + }); + + return resultingString; }; } From e5f75f2f6188d5f0480b53d9d367a3e6f6b1f552 Mon Sep 17 00:00:00 2001 From: AndrMar1939 Date: Mon, 7 Aug 2023 09:42:32 +0300 Subject: [PATCH 2/5] solution --- src/arrayMethodJoin.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index e97a20045..b74ea556a 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -6,7 +6,7 @@ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { const currentArrayLength = this.length; - const joinSeparator = separator + ''; + const joinSeparator = String(separator); let resultingString = ''; if (!currentArrayLength) { @@ -14,15 +14,17 @@ function applyCustomJoin() { } this.forEach((item, index) => { - /* const stringCharacter = item ?? '' is the best choice, - but I couldn`t disable eslint for the nullish operator */ - const stringCharacter = item === null || item === undefined - ? '' - : item; + let stringCharacter = item; - currentArrayLength - 1 === index - ? resultingString += stringCharacter - : resultingString += stringCharacter + joinSeparator; + if (item === null || item === undefined) { + stringCharacter = ''; + } + + if (currentArrayLength - 1 === index) { + resultingString += stringCharacter; + } else { + resultingString += stringCharacter + joinSeparator; + } }); return resultingString; From b1ad3d6b7d8943e8c781b6a5c7437568996facb2 Mon Sep 17 00:00:00 2001 From: AndrMar1939 Date: Tue, 8 Aug 2023 16:11:34 +0300 Subject: [PATCH 3/5] add solution --- src/arrayMethodJoin.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index b74ea556a..4f24b426d 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -5,14 +5,9 @@ */ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { - const currentArrayLength = this.length; const joinSeparator = String(separator); let resultingString = ''; - if (!currentArrayLength) { - return resultingString; - } - this.forEach((item, index) => { let stringCharacter = item; @@ -20,11 +15,13 @@ function applyCustomJoin() { stringCharacter = ''; } - if (currentArrayLength - 1 === index) { + if (this.length - 1 === index) { resultingString += stringCharacter; - } else { - resultingString += stringCharacter + joinSeparator; + + return; } + + resultingString += stringCharacter + joinSeparator; }); return resultingString; From 07c26639e7cbc964c7db6711ece23f62b69c0497 Mon Sep 17 00:00:00 2001 From: AndrMar1939 Date: Tue, 8 Aug 2023 16:26:47 +0300 Subject: [PATCH 4/5] add solution --- src/arrayMethodJoin.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 4f24b426d..11f65dedf 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -5,24 +5,24 @@ */ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { - const joinSeparator = String(separator); let resultingString = ''; + const joinSeparator = String(separator); - this.forEach((item, index) => { - let stringCharacter = item; + for (let i = 0; i < this.length; i++) { + let stringCharacter = this[i]; - if (item === null || item === undefined) { + if (stringCharacter === null || stringCharacter === undefined) { stringCharacter = ''; } - if (this.length - 1 === index) { + if (this.length - 1 === i) { resultingString += stringCharacter; - return; + break; } resultingString += stringCharacter + joinSeparator; - }); + } return resultingString; }; From f9f5dda8760d79f1e2f30052eb111437b5f085a9 Mon Sep 17 00:00:00 2001 From: AndrMar1939 Date: Tue, 8 Aug 2023 16:31:37 +0300 Subject: [PATCH 5/5] add solution --- src/arrayMethodJoin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 11f65dedf..380ecd242 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -6,7 +6,6 @@ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { let resultingString = ''; - const joinSeparator = String(separator); for (let i = 0; i < this.length; i++) { let stringCharacter = this[i]; @@ -21,7 +20,8 @@ function applyCustomJoin() { break; } - resultingString += stringCharacter + joinSeparator; + resultingString += stringCharacter; + resultingString += separator; } return resultingString;