Ruby Ruby Basics Conditionals A Better "check_speed" Method

Chris Drummond
Chris Drummond
943 Points

This version of the check_speed only prints "speed OK" if the speed is exactly 55 miles per hour. It would be better to

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".

Here is the code I have tried but I'm missing something. Any help is appreciated.

def check_speed(speed) if speed < 45 puts "too slow" elsif speed >= 45 && <= 60 puts "speed OK" else speed > 60 puts "too fast" end end

program.rb
def check_speed(speed)
  if speed < 45
    puts "too slow"
  elsif speed >= 45 || >= 60
    puts "speed ok"
  else speed > 60
    puts "too fast"
  end
end

speed(53)

1 Answer

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,377 Points

You can try it in a workspace and errors will be presented that should walk you through it. Mainly this line:

elsif speed >= 45 || >= 60

You'll want to check speed <= 60

Also the function is check_speed() so this line won't work

speed(53)
Chris Drummond
Chris Drummond
943 Points

Hi Kris...I posted the wrong code. I meant to post this below:

def check_speed(speed) if speed < 45 puts "too slow" elsif speed >= 45 && <= 60 puts "speed ok" else speed > 60 puts "too fast" end end