From a7cbe0d5fd00e6dfa17a9285cacd7bb8211b1fd3 Mon Sep 17 00:00:00 2001 From: patryk177m Date: Sun, 30 Jul 2023 21:26:35 +0200 Subject: [PATCH] solution --- src/arrayMethodJoin.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 3a62201c1..9e26c40b4 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -4,8 +4,29 @@ * Implement method join */ function applyCustomJoin() { - [].__proto__.join2 = function(separator) { - // write code here + [].__proto__.join2 = function(separator = ',') { + let joinArray = ''; + let secondSeparator = separator; + + if (separator === null) { + secondSeparator = 'null'; + } + + for (let i = 0; i < this.length; i++) { + if (this[i] !== null && this[i] !== undefined) { + if (i === this.length - 1) { + joinArray += this[i]; + } else { + joinArray += this[i] + secondSeparator; + } + } else { + if (i !== this.length - 1) { + joinArray += secondSeparator; + } + } + } + + return joinArray; }; }