Median Paycheck: Solution Review
Understand how to apply RamdaJS functions to calculate median paychecks from salary data. Learn practical steps including filtering salaries, finding medians, dividing for monthly amounts, and formatting values in a functional programming style.
We'll cover the following...
I hope you didn’t cheat and look at the HOFs section to find this snippet. :D
Either way, let’s list the steps
- Get salaries
- Reject anything below $100,000
- Get the median
- Calculate monthly paycheck (amount / 12 months)
- Format dollars (USD)
I think we’re comfortable enough to start with a Ramda solution
Remember, pluck('salary') is equivalent to map(prop('salary')).
And R.lte is great for filtering the salaries.
Ramda has a divide function, but it doesn’t work as expected.
$0.00?! That doesn’t look right. Let’s inspect with tap.
Aha! We’re dividing 12 by 608702.5 and getting a tiny decimal that rounds to $0.00! But we want to flip that division! Sounds like a job for Ramda’s flip function.
Looks good to me! flip takes a function and returns a new one with the first two arguments reversed.
https://ramdajs.com/docs/#flip
Again, I wouldn’t do this in the real world. The point’s to expose you to Ramda’s toolkit and let you decide what’s best for your application.