Here are the instructions:
We’ll be rebuilding the native .join array method. The join function should work as follows:
// If the input is empty, return an empty string
join([]) // ''
// If there is no separator, separate by commas
join([ 'a', 'b', 'c' ]) // 'a,b,c'
// If there is a separator, separate by that string
join([ 'a', 'b', 'c' ], '-') // 'a-b-c'
// If there is only one item, return just that item
join([ 'a' ], '---') // 'a'
I’m just a bit confused on this line:
if (separator && array[i] !== array[array.length-1])
Here is my code:
function join (array, separator) {
let str = '';
for (let i = 0; i < array.length; i++) {
str += array[i];
if (separator && array[i] !== array[array.length-1]) {
str += separator;
} else if (array[i] !== array[array.length-1]) {
str += ',';
}
}
return str;
}