Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.