Hi,
I have the following exercise:
Write a function list
that takes an array of words and returns a string by concatenating the words in the array, separated by commas and - the last word - by an ‘and’. An empty array should return an empty string.
The solution should include the use of join().
My js:
var list = function (x) {
var lgth = x.length -2; //figure out where to split the array
var lastpart = x.slice(lgth); // slice the last 2 items of the array
var laststring= lastpart.join (' and '); //make a string with and between
var firstpart = x.slice(0,lgth); //slice the first part of the array
var firststring = firstpart.join(); //make a string with , between each item
var totalarray = [firststring, laststring]; // combine to an new array
var totalstring = totalarray.toString(); //change to a string
return totalstring;
};
The trouble starts when the array is empty, it returns ’ , ’ instead of ’ '.
I do not now if the code i have written is the correct way to resolve the exercise so i could use some help.
Thanks, Kris
Please use meaningful variable names to help us and help you understand what your code is trying to do.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
Note: Backticks are not single quotes.

Changed it a bit. Thanks for the help. Hope this is more readable?
I think the code looks overcomplicated for what seems like such a simple exercise.
I think there were some incomplete variable renamings.
Is this right
var list = function (x) {
var lgth = x.length - 2; // figure out where to split the array
var lastPart = x.slice(lgth); // slice the last 2 items of the array
var lastString = lastPart.join (' and '); // make a string with and between
var firstPart = x.slice(0, lgth); // slice the first part of the array
var firstString = firstPart.join(); // make a string with , between each item
var totalArray = [firstString, lastString]; // combine to an new array
var totalString = totalArray.toString(); // change to a string
return totalString;
};
console.log(list(["a", "b", "c"]))
Yes indeed, i have edited it again 
I don’t know how committed you are to this approach, but you can do this with a single .join()
and no slicing.
Hint: if you have an empty array and call .join(', ')
on it, the result will be an empty string.
1 Like
I could be wrong, but I’d guess that you want to add spaces between the items, which would mean you’d want to use a custom deliminator for join()
.
More specific to your question though, you could return
early if you want to return an empty string for an empty array.
Yeah, I used @ArielLeslie’s hint and it makes a smaller, cleaner code. Highly recommend, 7/5, all hail, etc
Hm, i do not understand. If i just use :
var result = x.join(' , ');
result will be (if x = [‘John’,‘terry’,‘paul’]) = ‘John,Terry, Paul’
An empty x will result in an empty string,
but i do not have an AND between the last 2 items. And that one seems to be the real problem in this exercise.
Sure, but you can modify the last element of the array to add the and before you join.
Ok, but how do i do that? If i add something to the array with ‘push’ js puts it at the end. And even then i still have to get rid of the last comma?
You can replace the contents of the last element.
i.e.
["apples", "bananas", "clementines"]
would be come
["apples", "bananas", "and clementines"]
You can replace individual elements in an array by using
myArray[42] = thatNewValue;