Cheapest Item: Solution Review
Understand how to apply RamdaJS functional programming concepts to solve practical problems by sorting items and extracting data using pipe, prop, head, and sortBy functions. Learn to write cleaner and more expressive code to identify the cheapest item.
We'll cover the following...
Sorting
Getting the cheapest item implies you’re sorting by price. After that grab the item’s name.
A vanilla solution might look like this
A $10 carrot is the cheapest item. What kind of grocery store is this?!
Anyways, we see the order of operations
- Sort by price
- Grab the first or last item (depending on how you sorted)
- Return its name
We know pipe and prop from the last exercise and those seem like good candidates here. Ramda also carries a sort function.
This works, but a bit awkwardly. I’m not fond of how we’re returning the head element
(list) => list[0]
Let’s replace that with Ramda’s head function. It returns a list’s head element.
Much better. Pat yourself on the back if you got this.
But did you know about sortBy?
It takes a function that describes how the data should be sorted. Useful if your sorts involve any complex logic.