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 trialJon Jaffe
1,217 PointsWhere did I go wrong? Having trouble understanding methods, arguments, and return. Ruby Operators and Control Structures
I am having trouble understanding methods, arguments and when to use return. I've pasted the prompt below. How did I mess up my code?
The method below checks the speed of a car and returns a string value: either "safe" or "unsafe". Return "safe" if: The car_speed passed in as an argument is greater than or equal to the number 40. The car_speed passed in as an argument is less than or equal to the number 50. Otherwise, return "unsafe". Hint: You should use the && logical operator to accomplish this task. :)
def check_speed(car_speed)
# write your code here
if (car_speed >= 40) && (car_speed <= 50)
return "safe"
elsif
return "unsafe"
end
2 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Jon,
You've pretty much got it. There are just a couple things:
- Because you are checking a condition that can only return 2 values, you cannot use
elsif
. That's used when checking multiple (more than 2) potential returns. When there is only the 2 possibilities (here it's True or False), you have to useelse
. - You didn't close the
if
statement. (Missing anend
)
Just fix those up and you're good to go.
Keep Coding! :)
Jon Jaffe
1,217 PointsThank you Jason!