diff --git a/docs/burger-discounts/Discount.re b/docs/burger-discounts/Discount.re
index 1ef57bc0..3b469909 100644
--- a/docs/burger-discounts/Discount.re
+++ b/docs/burger-discounts/Discount.re
@@ -148,7 +148,7 @@ let _ = {
// #region improved-get-free-burger
// Buy 2 burgers, get 1 free
let getFreeBurger = (items: array(Item.t)) => {
- let burgers =
+ let prices =
items
|> Js.Array.filter(~f=item =>
switch (item) {
@@ -160,13 +160,34 @@ let getFreeBurger = (items: array(Item.t)) => {
|> Js.Array.map(~f=Item.toPrice)
|> Js.Array.sortInPlaceWith(~f=(x, y) => - compare(x, y));
- switch (burgers[0], burgers[1]) {
- | (Some(_), Some(cheaperPrice)) => Some(cheaperPrice)
- | (None | Some(_), None | Some(_)) => None
+ switch (prices[1]) {
+ | None => None
+ | Some(cheaperPrice) => Some(cheaperPrice)
};
};
// #endregion improved-get-free-burger
+ignore(getFreeBurger);
+
+// #region final-get-free-burger
+// Buy 2 burgers, get 1 free
+let getFreeBurger = (items: array(Item.t)) => {
+ let prices =
+ items
+ |> Js.Array.filter(~f=item =>
+ switch (item) {
+ | Item.Burger(_) => true
+ | Sandwich(_)
+ | Hotdog => false
+ }
+ )
+ |> Js.Array.map(~f=Item.toPrice)
+ |> Js.Array.sortInPlaceWith(~f=(x, y) => - compare(x, y));
+
+ prices[1];
+};
+// #endregion final-get-free-burger
+
// #region get-half-off-one
// Buy 1+ burger with 1 of every topping, get half off
let getHalfOff = (items: array(Item.t)) => {
diff --git a/docs/burger-discounts/index.md b/docs/burger-discounts/index.md
index 3772707d..87fef9dd 100644
--- a/docs/burger-discounts/index.md
+++ b/docs/burger-discounts/index.md
@@ -477,9 +477,23 @@ Use [Js.Array.map](https://melange.re/v3.0.0/api/re/melange/Js/Array/#val-map)
<<< Discount.re#improved-get-free-burger
+Since the chain of function invocations now results in an array of `float`s, we
+rename the variable from `burgers` to `prices`. We only need to match on the
+second element of `prices` because if it exists, then a first element must also
+exist (but we don't need to know its value).
+
:::
-2. Add new function `Discount.getHalfOff` that gives you a discount of
+2. `Discount.getFreeBurger` can still be improved. Refactor it to remove
+the switch expression entirely.
+
+::: details Solution
+
+<<< Discount.re#final-get-free-burger
+
+:::
+
+3. Add a new function `Discount.getHalfOff` that gives you a discount of
half off the entire meal if there’s at least one burger that has one of every
topping.
@@ -495,7 +509,7 @@ Use [Js.Array.some](https://melange.re/v3.0.0/api/re/melange/Js/Array/#val-some)
:::
-3. Update `Discount.getHalfOff` so that it returns a discount of one half
+4. Update `Discount.getHalfOff` so that it returns a discount of one half
off the entire meal if there’s at least one burger that has **at least** one of
every topping. Also add a couple of tests for this function in `DiscountTests`.
@@ -516,9 +530,6 @@ to see how the tests are implemented. Note the use of a submodule to group the
:::
-4. tbd
-
-
-----
View [source
diff --git a/src/burger-discounts/Discount.re b/src/burger-discounts/Discount.re
index 7962d592..6175f39e 100644
--- a/src/burger-discounts/Discount.re
+++ b/src/burger-discounts/Discount.re
@@ -1,6 +1,6 @@
// Buy 2 burgers, get 1 free
let getFreeBurger = (items: array(Item.t)) => {
- let burgers =
+ let prices =
items
|> Js.Array.filter(~f=item =>
switch (item) {
@@ -12,10 +12,7 @@ let getFreeBurger = (items: array(Item.t)) => {
|> Js.Array.map(~f=Item.toPrice)
|> Js.Array.sortInPlaceWith(~f=(x, y) => - compare(x, y));
- switch (burgers[0], burgers[1]) {
- | (Some(_), Some(cheaperPrice)) => Some(cheaperPrice)
- | (None | Some(_), None | Some(_)) => None
- };
+ prices[1];
};
// Buy 1+ burger with 1+ of every topping, get half off