Created
July 17, 2016 06:35
-
-
Save swaathi/11bbb949d763205ea4c6ec0b734a09ae to your computer and use it in GitHub Desktop.
Convert an array into a comma seperated sentence.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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