Welcome to the Treehouse Community
Looking to learn something new?
Finding Array Items help
What are the issues with my code? I can't seem to figure this one out. I've watched the preceding video several times now and don't see where I went wrong.
def find_index(name) index = 0 found = false todo_items.each do |x| if x.name == name found = true end index += 1 break if found end if found return index else return nil end end
Seth Reece32,867 Points
It looks like you are breaking the loop out after you increment index, so if an item is found, your code will return the index of that item plus one. I got a pass with:
def find_index(name) index = 0 found = false todo_items.each do |item| if item.name == name found = true break end index += 1 end if found == true return index else return nil end end