Created
November 30, 2020 04:48
-
-
Save KMurphs/f9698897cc23aa90b5f17392958191c0 to your computer and use it in GitHub Desktop.
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
/* Simple function that calculates tax for a certain amount */ | |
const calculateTax = function(amount, taxRate){ | |
return amount * taxRate; | |
} | |
/* | |
Straightforward Higher Order Function that prepends a currency symbol to | |
a function. | |
*/ | |
const prependCurrencySymbol = function(someFunction, symbol = "$"){ | |
/* | |
Builds an augmented version of the "someFunction" provided that | |
append the currency symbol | |
*/ | |
const modifiedFunction = function(...args){ | |
return `${symbol}${someFunction(...args)}`; | |
} | |
/* Return a new function that is the augmented version of the original function */ | |
return modifiedFunction; | |
} | |
/* | |
These function use our higher order function to modify and customize the | |
behaviour of the original function "calculateTax" | |
*/ | |
const calculateTaxWithDollarSymbol = prependCurrencySymbol(calculateTax, "$") | |
const calculateTaxWithPoundSymbol = prependCurrencySymbol(calculateTax, "£") | |
calculateTax(10, 0.14) | |
// 1.4000000000000001 | |
calculateTaxWithDollarSymbol(10, 0.14) | |
// "$1.4000000000000001" | |
calculateTaxWithPoundSymbol(10, 0.14) | |
// "£1.4000000000000001" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment