Created
March 23, 2021 16:34
-
-
Save oscarryz/f23ce53d939cdca42bf0df60bdb9eadc to your computer and use it in GitHub Desktop.
Basic "while" loop in elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Password do | |
@doc """ | |
Consider a basic password loop | |
it exists with a positive result if the user chooses the right password | |
It exists with a negative result if theuser is wrong three times | |
How would you code this loop | |
""" | |
def guess(attempts \\ 0) | |
def guess(attempts) when attempts < 3 do | |
guess = IO.gets "Guess: " | |
if guess == "si\n" do | |
{:ok, "You've got it"} | |
else | |
IO.puts("Try again") | |
guess(attempts+1) | |
end | |
end | |
def guess(3) do | |
{:nope, "Max attempts reached"} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment