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

Steven Fried
PLUS
Steven Fried
Courses Plus Student 5,638 Points

I have not been able to complete this challenge. What is the correct answer?

My response would be:

if (car_speed >= 40) && (car_speed <= 50) 
return safe
else
return unsafe


def check_speed(car_speed)
  if (car_speed# write your code here

end
John Zukowski
John Zukowski
1,851 Points

My suggestion would be to use control structures ... for example:

def check_speed(car_speed)
  if (car_speed >= 40)
    return "safe"
  else
    return "unsafe"
  end
end

1 Answer

Hi Steven

You need to test car_speed against two things (being greater or equal to 40 or less than/equal to 50) - that makes it "safe". The other option is "unsafe" but that's the simple bit!

So, to test car_speed against 40 would look like car_speed >= 40. To test against 50 looks like car_speed <= 50. Using an if statement, as you have done and the && operator to check two conditions (as you have done), you then need to return a string (surround with inverted commas) and conclude the if statement with the keyword end.

So, you've missed the string piece and the end keyword. the rest of what you've done is fine! The code looks like:

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

I hope that helps.

Steve.

P.S. You can omit the return keyword - it isn't necessary but it does help with readability of the code.