Consider this code:
def itemsinlist(list, item)
list.each do |x|
if item == x
return true
else
return false
end
end
end
groceries = ["milk", "juice", "tacos"]
puts itemsinlist(groceries, "milk")
#This returns false..... why.....?
#Now consider this code:
def itemsinlist(list, item)
list.each do |x|
if item == x
return true
end
end
end
groceries = ["milk", "juice", "tacos"]
puts itemsinlist(groceries, "soup")
#Since "soup" is not in the array, it just returns the WHOLE array..
#obviously I don't want it to return the whole array if my item is not in the list..
#Could someone example to me why these 2 problems are giving me these answers?