Created
September 4, 2014 22:12
-
-
Save amitkumar/076c15c6d66273b066e1 to your computer and use it in GitHub Desktop.
Join an array of words with commas and a final "and"
This file contains hidden or 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
function joinWithCommas (array){ | |
var result = ''; | |
$.each(array, function(index, label){ | |
if (index === 0){ | |
} else if (index < (array.length - 1)){ | |
result += ', '; | |
} else { | |
result += ' and '; | |
} | |
result += label; | |
}); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment