Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialdvee
4,850 Pointsreturn @todo_items.index(name) Doesn't work
The following line of code should return the index of the array or nil if nothing is there: return @todo_items.index(name) In the off chance it didn't return nil I modified it to use if/else to return nil or the index. Neither worked. What am I missing?
1 Answer
Travis Howk
7,169 PointsSo my answer here was a bit more basic than what you were trying to do. I chose to iterate over each of the items in the array and check if they were equal to the passed in parameter name. It looks like this:
def find_index(name)
index = 0
found = false
todo_items.each do |item|
if item.name == name
found = true
return index
end
if found
break
else
index += 1
end
end
return nil
end