From 9ae93ff6cda3ef42234a1753ea8ed99e7fc41c67 Mon Sep 17 00:00:00 2001 From: Bahtijar Date: Sun, 20 Aug 2023 21:28:21 +0200 Subject: [PATCH] stage-1 --- src/arrayMethodJoin.js | 59 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 3a62201c1..81f93c859 100644 --- a/src/arrayMethodJoin.js +++ b/src/arrayMethodJoin.js @@ -1,11 +1,62 @@ 'use strict'; -/** - * Implement method join - */ function applyCustomJoin() { + /** + * @param {string | undefined} separator + * @returns {string} */ [].__proto__.join2 = function(separator) { - // write code here + let inerSeparator; + let joined = ''; + let temp = ''; + + if (this.length === 0) { + return joined; + } + + switch (separator) { + case null: + inerSeparator = 'null'; + break; + + case undefined: + inerSeparator = ','; + break; + + default: + inerSeparator = separator; + } + + for (let i = 0, last = this.length - 1; ; i++) { + temp = this[i]; + + switch (temp) { + case null: + case undefined: + temp = ''; + break; + case true: + temp = 'true'; + break; + case false: + temp = 'false'; + break; + case NaN: + temp = 'NaN'; + break; + default: + break; + } + + joined += temp; + + if (i === last) { + break; + } + + joined += inerSeparator; + } + + return joined; }; }