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 Operators and Control Structures Logical Operators Multiple Conditions with && and ||

Patrick Shushereba
Patrick Shushereba
10,911 Points

Ruby Operators and Control Structures

I need help figuring out what is wrong with my code:

 def check_speed(car_speed)

  speed = car_speed.to_i

  if (speed > 40)  && (speed <= 50)

    puts "safe"

  end

end

The message that I'm getting is that the check_speed method is not giving the correct output.

Bogere Sanders
Bogere Sanders
2,195 Points

why did you add "speed = car_speed.to_i" ?

2 Answers

Hi Patrick,

  • Need to add equal (=) that would be maximum up to 40. Also other way, you can do this speed > 39 instead 40.
  • return value for "safe" string according to the question, puts vs return are not the same thing.
 def check_speed(car_speed)

  speed = car_speed.to_i

  if (speed >= 40)  && (speed <= 50)

    return "safe"

  end

end

Hope that helps. ;)

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

Hi Bogere,

The to_i method is added to convert the car_speed argument from an input string to an actual number.

Hope that helps.