Ruby Ruby Basics Conditionals A Better "check_speed" Method

Corina Feraru
Corina Feraru
6,257 Points

Cannot pass the test even thou the exercise passes in IRB

[14] pry(main)> def check_speed(speed) [14] pry(main)* if speed < 45 [14] pry(main)* return "too slow" [14] pry(main)* elsif speed == (45..60) [14] pry(main)* return "speed OK" [14] pry(main)* elsif speed > 60 [14] pry(main)* return "too fast" [14] pry(main)* end [14] pry(main)* end => :check_speed [15] pry(main)> check_speed(20) => "too slow" [16] pry(main)>

def check_speed(speed)
if speed < 45
  return "too slow"
  elsif speed == (45..60)
   return "speed OK"
  elsif speed > 60
   return "too fast"


2 Answers

Jason Anders
Jason Anders
Treehouse Moderator 144,350 Points

Hi Corina,

With the Challenges on Treehouse, just because it works on your machine, does not mean that it meets the requirements of the challenge as set on in the instructions. Instructions are always very specific and need to be followed exactly. With that in mind, there are a few things a bit off.

The biggest error is with the second conditional, while a range could be used, as is, the syntax is not correct. Explaining ranges is beyond the scope of this question, so I will leave the research on that up to you. For now, the challenge is not looking for a range, it's actually looking for a conditional check using the AND (&&) operator. The reason this didn't error out on your machine is because you called the method with 40. Try calling it with 45 and see what happens.

Second, the instructions specifically say to print the strings, but you are using return.

Third, for the final check, you don't use an elsif with a value, you simply use an else statement with no checks. If the value fails the above two checks, then is must be greater than 60.

Finally, the instructions do not say to call the function. This is being done behind the scenes to test your code, so when you call the method without being asked to, the challenge will usually throw the Bummer because it confused the checker.

Just fix those up and the rest looks good :thumbsup:

Keep Coding! :) :dizzy:

Corina Feraru
Corina Feraru
6,257 Points

Hello Jason, thank you so much for the detailed answer you are very kind, now is working, i knew i had to follow the precise instructions but i couldnt get out of the range approach i couldnt see the alternative with &&...

Once again thanks a lot, Corina