Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Booleans Build a Simple Todo List Program Finding Array Items

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

Now my code has disappeared and the code included with the challenge has changed completely (only included the specific method but now when revisiting, the whole class is included) - what is going on?

1 Answer

Seth Reece
Seth Reece
32,867 Points

Hi James,

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