Skip to content

Instantly share code, notes, and snippets.

@helabenkhalfallah
Last active May 15, 2021 23:11
Show Gist options
  • Save helabenkhalfallah/75414bb1a81329aa4c9ee4770748754d to your computer and use it in GitHub Desktop.
Save helabenkhalfallah/75414bb1a81329aa4c9ee4770748754d to your computer and use it in GitHub Desktop.
JS Curried Function (Converter 2)
const converterOld2 = (toUnit) => (factor) => (offset) => (input) => {
const converterOffset = offset || 0;
return [((converterOffset + input) * factor).toFixed(2), toUnit].join(' ');
};
const kmConverterWithUnit = converterOld2('km'); // function with a single params
const kmConverterWithFactor = kmConverterWithUnit(1.60936); // function with a single params
const kmConverter = kmConverterWithFactor(undefined); // function with a single params
console.log('kmConverter : ', kmConverter);
console.log('kmConverter(10) : ', kmConverter(10)); // "16.09 km"
const convert10MilesToKm11 = converterOld2('km')(1.60936)(undefined)(10);
const convert20MilesToKm12 = converterOld2('km')(1.60936)(undefined)(20);
const convert30MilesToKm13 = converterOld2('km')(1.60936)(undefined)(30);
console.log('convert10MilesToKm11 : ', convert10MilesToKm11); // "16.09 km"
console.log('convert20MilesToKm12 : ', convert20MilesToKm12); // "32.19 km"
console.log('convert30MilesToKm13 : ', convert30MilesToKm13); // "48.28 km"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment