-1
I’m making a loop that will check my list. The problem is that the loop doesn’t stop when I get where I want, then it ends up showing everything that has been checked.
defmodule Tester do
def a(list,p) do
for x <- list do
if(x == p) do
IO.puts "Number #{p} belongs to the list"
else
IO.puts "Number #{p} does not belong to the list"
end
end
end
end
This is the output when calling the function with the list [1,2,3]
and a number 3
.
The number 3 does not belong to the list
The number 3 does not belong to the list
The number 3 belongs to the list
Already tried using the command
break
orreturn
?– Danizavtz
"not when I get where I want" and where is what you want? Where should be stopping?
– Woss
If the idea is just to check whether
p
is on the list, can dop in list
. That expression will returntrue
if you are on the list, orfalse
otherwise.– Woss
@Danizavtz Turn and break, no elixir as far as I know.
– LuizF6843
@Luizf6843 our, what legal language!
– Danizavtz
@Woss I explained a little wrong, I’ll redo it. What I want, is for him to check if he has the number inside the list. Right. but instead of returning only "the number is in the list", it returns everything that has been checked, ex ["the number is not in the list',"the number is not in the list", "the number is in the list" ]
– LuizF6843
I managed to fix the "problem", with a help. Thanks there who answered me! :)
– LuizF6843