From 9b8417286bb7a1a13820c4789c83d20c83a9604d Mon Sep 17 00:00:00 2001 From: AntonFof Date: Mon, 7 Apr 2025 16:49:15 +0300 Subject: [PATCH] mySolution --- src/arrayMethodJoin.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 3a62201c..c61fac8b 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 = ',') { + let joinedString = ''; + + for (let i = 0; i < this.length; i++) { + const element = this[i]; + const lastElement = i === this.length - 1; + + /** + * wanted to use `joinedString += element ?? ''`, + * meaning without any `if()` construction, + * but mate's eslint unables me to do it + */ + if (element !== undefined && element !== null) { + joinedString += element; + } + + if (!lastElement) { + joinedString += separator; + } + } + + return joinedString; }; }