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 trialkevinthecoder
10,791 PointsUnable to get past Has_value? Challenge
Alright, this one has me stumped. I have re-played two different videos a total of three times plus carefully looked at my code for syntax errors, missing quotes, etc. I even tried single and double = signs (it should be ==). What am I doing wrong? My if statement should be good. Help!
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if grocery_item.has_value?("Bread") == true
grocery_item = {"food" => true}
end
4 Answers
William Li
Courses Plus Student 26,868 PointsHi, kevinthecoder
# there's a problem with this line
grocery_item = {"food" => true}
# instead of just setting the value for "food" key
# you're essentially assigning grocery_item to a completely new Hash
# change it to
grocery_item ["food"] = true
Luke Buśk
21,598 PointsThis is how it should look:
if grocery_item.has_value?("Bread")
grocery_item["food"] = true
end
You do not need to specify "== true" because "has_value?" already returns true if such item is present.
kevinthecoder
10,791 PointsGuys, this still did NOT work. I tried both with brackets and parenthesis and it still doesn't work. I'll try to paste below (I'm trying to stick to the same forum post and not a new forum post):
if grocery_item.has_value?("Bread") = true
grocery_item["food"] = true
end
Luke Buśk
21,598 PointsRemove the " = true" in the first line. Remember that one equal sign assigns a variable, two equal signs (==) checks equality. And as i mentioned earlier You do not need to specify it, the has_value? already returns true if it finds "Bread".
kevinthecoder
10,791 PointsAh, OK....I think I got it now! I passed this Challenge and the next one....thanks for the help! I'm on to the last new section (grocery list).