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 Basics Conditionals A Better "check_speed" Method

Ediju Rodrigues
Ediju Rodrigues
5,214 Points

This version of the check_speed only prints "speed OK" if the speed is exactly 55 miles per hour.

This version of the check_speed only prints "speed OK" if the speed is exactly 55 miles per hour. It would be better to allow a range of speeds. Update check_speed with this logic: If we pass a speed of less than 45, check_speed should print "too slow". If we pass a speed of 45 to 60, check_speed should print "speed OK". If we pass a speed of greater than 60, check_speed should print "too fast".

Does anyone knows how to do this?

program.rb
def check_speed(speed)
  if speed <= 45
    puts "too slow"
  end

  if speed == 55
    puts "speed OK"
  end
  if speed > 60
    puts"too fast"
  end

end
Dimitri Fajardo
Dimitri Fajardo
Courses Plus Student 6,973 Points

You almost got it but it's in the wording that you have to look. Speed is below 45 so try and do this. speed > 45

After, If we pass a speed of 45 to 60. Mean between 45 and 60 so... speed <= 45 && speed <=60

an you know for the last one.

I don't remember if this test is after the else, elsif and if.

If it is there is a better solution which is

def check_speed(speed) if speed < 45 puts "too slow"

elsif speed >= 45 puts "speed OK"

else speed > 60 puts"too fast" end

end

3 Answers

def check_speed(speed)
  if speed < 45
    puts "too slow"
  end
  if speed > 60
    puts "too fast"
  end
  if speed > 44 && speed < 61
    puts "speed OK"
  end
end
Ediju Rodrigues
Ediju Rodrigues
5,214 Points

yoo thank you so much DImitri Fajardo \o/ you helped me a lot Will continue studying your example to understant this thing, else, elsif ect..I need to master this things) Good lucky with everything man, thank you again

Ediju Rodrigues
Ediju Rodrigues
5,214 Points

Dimitri Fajardo, the code you wrote didn't work too, it's almost there, it says: "We called check_speed with an argument greater than 60, but it didn't print "too fast"."

Thank you very much for the tips, i will continue trying to understand the logic of this exercices and different ways to do it!

Dimitri Fajardo
Dimitri Fajardo
Courses Plus Student 6,973 Points

I made a mistake I forgot an end there are 2 of them. I just did it it works

def check_speed(speed)
  if speed < 45
    puts "too slow"

  elsif speed > 60
    puts "too fast"

else speed >= 45
    puts "speed OK"
  end
end