Shopping List
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Setup
Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.
Text Shopping List
You are creating a shopping list app. Users have the ability to add items into their shopping_cart.
Each item is represented as a string.
In the Elixir cells below, use ++ and -- to add the items shown.
-
Add
"grapes","walnuts", and"apples"to theshopping_cart -
Add
"blueberries","chocolate", and"pizza"to theshopping_cart. -
Remove
"grapes"and"walnuts"from theshopping_cart -
Add three
"banana"s to theshopping_cart
Example solution
shopping_cart = []
shopping_cart = shopping_cart ++ ["grapes", "walnuts", "apples"]
shopping_cart = shopping_cart ++ ["blueberries", "chocolate", "pizza"]
shopping_cart = shopping_cart -- ["grapes", "walnuts"]
shopping_cart = shopping_cart ++ ["banana", "banana", "banana"]
Enter your solution below.
Text Shopping List With Quantities
Users of your shopping list app have asked that they be able to include the quantity of each item to make adding many items easier.
In the Elixir cell below, use a keyword list in the format [item: quantity] to add or remove
items from the shopping_cart
-
Add
1milkand12eggs -
Add
2bars_of_butterand10candies -
Remove
2bars_of_butter -
Remove
5candies(Notice5and not10!).
Example solution
shopping_cart = []
shopping_cart = shopping_cart ++ [milk: 1, eggs: 12]
shopping_cart = shopping_cart ++ [bars_of_butter: 2, candies: 10]
shopping_cart = shopping_cart -- [bars_of_butter: 2]
shopping_cart = shopping_cart -- [candies: 10]
shopping_cart = shopping_cart ++ [candies: 5]
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "shopping_list_reading"
"exercises" -> "shopping_list_exercise"
end
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, save_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(
progress_path,
Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
)
end
end)
form
Commit Your Progress
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.
$ git checkout -b shopping-list-exercise
$ git add .
$ git commit -m "finish shopping list exercise"
$ git push origin shopping-list-exercise
Create a pull request from your shopping-list-exercise branch to your solutions branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your teacher by including @BrooklinJazz in your PR description to get feedback.
You (or your teacher) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.
Up Next
| Previous | Next |
|---|---|
| Maps | Family Tree |