From 0487bd0306665789cd3372c6baac64906dcbd4f0 Mon Sep 17 00:00:00 2001 From: Olha Momot Date: Fri, 4 Aug 2023 22:32:13 +0300 Subject: [PATCH 1/2] add task solution --- src/arrayMethodJoin.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 3a62201c1..d92a63d79 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -4,8 +4,26 @@ * Implement method join */ function applyCustomJoin() { - [].__proto__.join2 = function(separator) { - // write code here + [].__proto__.join2 = function(separator = ',') { + let resultString = ''; + + if (this.length === 0) { + return ''; + } + + for (let i = 0; i < this.length - 1; i++) { + if (this[i] !== undefined && this[i] !== null) { + resultString += this[i]; + } + + resultString += separator; + } + + if (!this[this.length - 1]) { + return resultString; + } + + return resultString + this[this.length - 1]; }; } From 05b29d057bacbd6aed6dafa4134fd6adb356b304 Mon Sep 17 00:00:00 2001 From: Olha Momot Date: Tue, 8 Aug 2023 19:09:59 +0300 Subject: [PATCH 2/2] fixed bug & added variables --- src/arrayMethodJoin.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index d92a63d79..0e90ae953 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -7,19 +7,19 @@ function applyCustomJoin() { [].__proto__.join2 = function(separator = ',') { let resultString = ''; - if (this.length === 0) { - return ''; - } - for (let i = 0; i < this.length - 1; i++) { - if (this[i] !== undefined && this[i] !== null) { - resultString += this[i]; + const element = this[i]; + + if (element !== undefined && element !== null) { + resultString += element; } resultString += separator; } - if (!this[this.length - 1]) { + const lastElement = this[this.length - 1]; + + if (lastElement === undefined || lastElement === null) { return resultString; }