Skip to content

Instantly share code, notes, and snippets.

@shiftb
Created August 15, 2011 19:47
Show Gist options
  • Save shiftb/1147616 to your computer and use it in GitHub Desktop.
Save shiftb/1147616 to your computer and use it in GitHub Desktop.
Rails to_sentence implemented in JavaScrip
/* Given an array of strings it returns a sentence. */
function toSentence(arr) {
arr = arr.slice(0);
switch (arr.length) {
case 0:
return "";
case 1:
return arr.pop();
default:
var lastItem = arr.pop();
return arr.join(', ') + " and " + lastItem;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment