We're trying to write a function that takes an array of strings and a separator, and joins all the strings separated by such separator.
We are not allowed to use the .join() method.
So far, I'm stuck trying to iterate though all the strings adding a separator between them each time.
I tried something like:
var aName = ['Frank','Vincent','Zappa'];
var join = (arr, separator = " ") => {
for(var i = 0; i < arr.length; i++) {
return arr[i] + separator + arr[i+1];
}
};
join(aName, ' ');
//output: "Frank Vincent"
//expected output: "Frank Vincent Zappa"
Thanks for your help.
arr[i] + separator + arr[i+1];will cause items to be repeated. Perhaps initalise a sting to the first element and the loop over the remaining elements and appendingseparator + arr[i]?