Skip to content

Instantly share code, notes, and snippets.

@swaathi
Created July 17, 2016 06:35
Show Gist options
  • Save swaathi/11bbb949d763205ea4c6ec0b734a09ae to your computer and use it in GitHub Desktop.
Save swaathi/11bbb949d763205ea4c6ec0b734a09ae to your computer and use it in GitHub Desktop.
Convert an array into a comma seperated sentence.
getNames: function(people) {
var length = people.length;
if (length == 0) {
return "";
} else if (length == 1) {
return people[0].name;
} else if (length == 2) {
return people[0].name + " and " + people[1].name;
} else {
var clonedPeople = people.slice(0);
var names = clonedPeople.splice(0, length - 1).map(function(person) {
return person.name;
});
}
var lastname = people[length-1].name;
return names.join(", ") + " and " + lastname;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment