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 trialCaleb Kleveter
Treehouse Moderator 37,862 PointsReturning boolean values: part 2.
The instructions for this challenge are as follows: "In the TodoList class, fill in the contains? method so that it returns a boolean value if the todo items array contains an item with the name argument."
With my code I get a bummer message that says try again!
class TodoList
attr_reader :name, :todo_items
def initialize(name)
@name = name
@todo_items = []
end
def add_item(name)
todo_items.push(TodoItem.new(name))
end
def contains?(name)
todo_items.each do |todo_arguments|
return todo_arguments.try_convert(name)
end
end
def find_index(name)
index = 0
found = false
todo_items.each do |item|
found = true if item.name == name
break if found
index += 1
end
if found
return index
else
return nil
end
end
end
If I try this code:
if index = find_index(name)
return true
end
I get this message: " Bummer! The contains?
method did not return a boolean value."
3 Answers
Salman Akram
Courses Plus Student 40,065 PointsHi Caleb,
So close I revise your code example, here's alternative way. It is much easier to read and understand easily
def contains?(name)
todo_items.each { |todo_arguments| return true if todo_arguments.name == name }
false
end
Stephen Printup
UX Design Techdegree Student 45,252 PointsI struggled with the same question and used this:
def contains?(name)
todo_items.each do |todo_item|
if todo_item.name == name
return true
else
return false
end
end
end
I think it's a bit closer to what the videos were teaching, but whatever works.
Caleb Kleveter
Treehouse Moderator 37,862 PointsThanks! Did I mis something in a video, because I don't recognize that code.
Minca Daniel Andrei
6,245 PointsThe logic is from the find_index
method