Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Guess the number within a range
defp midpoint a,b do div(a+b,2) end
def guess(my_choice, a.._) when my_choice == a do IO.puts "is it #{a}?" end
def guess(my_choice, _..b) when my_choice == b do IO.puts "is it #{b}?" end
def guess(my_choice, a.._) when my_choice < a do IO.puts "out of range" end
def guess(my_choice, _..b) when my_choice > b do IO.puts "out of range" end
def guess(my_choice, a..b) when my_choice > div((a+b),2) do
IO.puts "is it #{midpoint(a,b)}?"
guess my_choice, midpoint(a,b)..b
end
def guess(my_choice, a..b) when my_choice < div((a+b),2) do
IO.puts "is it #{midpoint(a,b)}?"
guess my_choice, a..midpoint(a,b)
end
def guess(my_choice, a..b) when my_choice == div((a+b),2) do
IO.puts "i got it. it is #{midpoint(a,b)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.