Skip to content

Instantly share code, notes, and snippets.

@whalesalad
Created July 19, 2009 09:44
Show Gist options
  • Select an option

  • Save whalesalad/149867 to your computer and use it in GitHub Desktop.

Select an option

Save whalesalad/149867 to your computer and use it in GitHub Desktop.
Number.prototype.pluralize = function(singular, plural) {
var a = '', b = 's';
if (singular && plural) { a = singular; b = plural; }
return (this == 1) ? a : b;
};
// Examples
function cherries(numCherries) {
alert('You have '+numCherries+' cherr'+numCherries.pluralize('y', 'ies')+' in mai bucket.');
};
cherries(3); // You have 3 cherries in mai bucket.
cherries(1); // You have 1 cherry in mai bucket.
// Combine all these little nuggets the grand finale!
foo = 3;
alert('You have '+foo.apnumber()+' cherr'+foo.pluralize('y', 'ies')+' in mai bucket.'); // You have one cherry in mai bucket.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment