Skip to content

Latest commit

 

History

History
178 lines (127 loc) · 5.49 KB

guessing_games.livemd

File metadata and controls

178 lines (127 loc) · 5.49 KB

Guessing Games

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Guess The Word

Create a word guessing game. Manually bind a guess and an answer. Return "Correct" if the guess is equal to the answer, and otherwise return "Incorrect."

Ensure your solutions works for both an incorrect and correct player guess.

Example Solution
guess = "hello"
answer = "hello"

guess == answer && "Correct!" || "Incorrect."

Enter your solution below.

Bonus: Guess The Random Word

We can use Enum.random/1 to pick a random value from a list.

Enum.random(["hello", "hi", "howdy"])

Armed with this knowledge, you're going to create a word guessing game.

  • Bind a variable guess to the player's guess as a string.
  • Bind a variable answer to a random word (you may create a list of random words to choose from)
  • If the guess matches the answer, return "Correct!", otherwise return "Incorrect.".
Example Solution
guess = "hello"
answer = Enum.random(["hello", "hi", "howdy"])

guess == answer && "Correct!" || "Incorrect."

Enter your solution below.

Bonus: Guess The Number

We can generate a random integer between two values by providing a range to Enum.random/1.

Enum.random(1..10)

Now let's build a number guessing game

  • Bind a variable answer to a random integer between 1 and 10.
  • Bind a variable guess to the player's guess.
  • If the guess equals answer, return "Correct!".
  • If the guess is lower than the answer, return "Too low!".
  • If the guess is higher than the answer, return "Too high!".
Example Solution

cond allows us to handle several different conditions.

guess = Enum.random(1..10)
answer = 7

cond do
  guess == answer -> "Correct!"
  guess < answer -> "Too low!"
  guess > answer -> "Too high!"
end

However, we can also solve this problem using purely boolean operators.

guess = Enum.random(1..10)
answer = 7

guess == answer && "Correct" || guess < answer && "Too low!" || guess > answer && "Too high!"

We can optionally simplify the solution by omitting the last condition. However, this could lead to unhandled bugs.

Enter your solution below.

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Guessing Games exercise"
$ git push

We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation