Skip to content

Instantly share code, notes, and snippets.

@oscarryz
Created March 23, 2021 16:34
Show Gist options
  • Save oscarryz/f23ce53d939cdca42bf0df60bdb9eadc to your computer and use it in GitHub Desktop.
Save oscarryz/f23ce53d939cdca42bf0df60bdb9eadc to your computer and use it in GitHub Desktop.
Basic "while" loop in elixir
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