Skip to content

Instantly share code, notes, and snippets.

@anthonybrown
Last active June 10, 2019 00:20
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 anthonybrown/f8bf14008f05ac9b1db55d1b058464c0 to your computer and use it in GitHub Desktop.
Save anthonybrown/f8bf14008f05ac9b1db55d1b058464c0 to your computer and use it in GitHub Desktop.
An example of functional programing using a higher order function
let formatCurrency = function( currencySymbol, decimalSeparator ) {
return function( value ) {
let wholePart = Math.trunc(value / 100);
let fractionalPart = value % 100;
if ( fractionalPart < 10 ) fractionalPart = '0' + fractionalPart;
return `${currencySymbol}${wholePart}${decimalSeparator}${fractionalPart}`;
}
}
let formatter = formatCurrency( '$', '.' );
console.log(formatter(0));
console.log(formatter(1209));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment