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 trial

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

Joshua Paulson
Joshua Paulson
37,085 Points

Wow this is a struggle, feeling like I'm very close but no cigar. Please help

What's wrong with my code, not sure what's wrong with this picture. Thought I was on point can someone please elaborate where I went wrong?

todo_list.rb
   def find_index(name)
    index = 0
    found = false

    todo_items.each do |todo_item|
        if todo_item.name == name
            found = true
            break;
        end
        index += 1
    end

    if found
        return index
    else
        return nil
    end
  end

1 Answer

Allison Hanna
Allison Hanna
36,222 Points

It looks like you have an extra end statement in there. This should work:

''' def find_index(name) index = 0 found = false

todo_items.each do |todo_item| 
    if todo_item.name == name
        found = true
        break;
    end
    index += 1
end

if found
    return index
else
    return nil
end

end '''

Allison Hanna
Allison Hanna
36,222 Points

Ah, sorry the formatting messed up.

def find_index(name)
    index = 0
    found = false

    todo_items.each do |todo_item| #add each method to loop through array
        if todo_item.name == name
            found = true
            break;
        end
        index += 1
    end

    if found
        return index
    else
        return nil
    end
  end
Joshua Paulson
Joshua Paulson
37,085 Points

Thank you much. I really appreciate that you explained rather than just posted the code. Excellent work!