diff --git a/src/arrayMethodJoin.js b/src/arrayMethodJoin.js index 3a62201c..0e90ae95 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 = ''; + + for (let i = 0; i < this.length - 1; i++) { + const element = this[i]; + + if (element !== undefined && element !== null) { + resultString += element; + } + + resultString += separator; + } + + const lastElement = this[this.length - 1]; + + if (lastElement === undefined || lastElement === null) { + return resultString; + } + + return resultString + this[this.length - 1]; }; }