Skip to content

Instantly share code, notes, and snippets.

@aradnom
Created October 9, 2015 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aradnom/5049f186bbca8383f57c to your computer and use it in GitHub Desktop.
Save aradnom/5049f186bbca8383f57c to your computer and use it in GitHub Desktop.
Splits passed credit number into quartets (groups of 4 digits).
/**
* Split a credit card into groups of four for more readable display.
*
* @param {Integer} number Number to split
*
* @return {String} Returns formatted number for display
*/
function splitCardNumber ( number ) {
return number
.toString()
.split( '' )
.reduce( function ( stack, next, index ) {
return ( index + 1 ) % 4 ? stack + next : stack + next + ' ';
}, '' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment