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

neoty888
neoty888
23,973 Points

Note sure what I'm doing wrong for this challenge. Can't get the 'check_speed' to pass for a speed less than 45.

I have the following logic:

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

When a speed of less than 45 is passed into the first if statement, it's rejected. Anyone have any ideas?

Thanks in advance

program.rb
def check_speed(speed)
  unless speed >= 45
    puts "too slow"
  end
  if  speed > 60
    puts "too fast"
  end
  if speed > 45 || speed < 60
    puts "speed OK"
  end
end
neoty888
neoty888
23,973 Points

nevermind, I found the answer

Ediju Rodrigues
Ediju Rodrigues
5,214 Points

What was the answer, can you tell me?

2 Answers

neoty888
neoty888
23,973 Points

Read through the problem, it's pretty straight forward. My mistake was essentially reading through the problem too fast. Below is the code that passed.

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

So, for example, if I pass in a speed of say 36, this would evaluate to "too slow" If I pass in a speed of '55', this would evaluate to "speed OK". this is because the speed is GREATER (>) than or EQUAL(==) to 45 AND (&&) LESS (<) than or EQUAL (==) to 60.

If you do this with "45", you'll notice that the first criteria being checked must also coincide with the logic in the second 'if' statement. If you do not add a qualifier of "greater than or equals to >=", the logic will evaluate to both 'too slow' and 'speed OK'. This will fail the check. There can only be one output. Adding the ">=" ensures that there is only one output for the speed check.

neoty888
neoty888
23,973 Points

Well, what I did was re-read the instructions. It's really a logic question. Just remember that each section all has to correspond to each other.

Ediju Rodrigues
Ediju Rodrigues
5,214 Points

well, that helped me a lot -.-'