Cheapest Item: Solution Review
Solution review.
We'll cover the following...
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.