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 The And (&&) Operator

Michael Bredthauer
Michael Bredthauer
13,429 Points

Trouble with this challenge in Ruby

I cant seem to get this challenge correct, can someone help me. I know how to make the if statement if(car_speed >= 40) && (car_speed <=50) but the rest I just cant get

ruby.rb
def check_speed(car_speed)
  # write your code here
  if(check_speed >= 40) && (check_speed <= 50)
    car_speed = "safe"
    return car_speed
end

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points
  • missed the end for if condition
  • also, you can return "safe" directly without assign it to a variable first.
def check_speed(car_speed)
  # write your code here
  if(car_speed >= 40) && (car_speed <= 50)
    "safe"
  end
end
Luke Buśk
Luke Buśk
21,598 Points

This is how it should look like:

def check_speed(car_speed)
  # write your code here
  if (car_speed >= 40) && (car_speed <= 50)
    return "safe"
  end

end

After You declare conditional structure like "IF", You need to always close it with "end". This is very important and You should keep an eye for it. Also You do not need to declare a new variable, Your task is to just return a string "safe".

Also please note that You do not need to specify "return" keyword in Ruby because the last line of any method is returned by default, i used it to make it more clear.