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

Ruby Multiple Conditions - Stage 3 Challenge 1

I cannot figure out the answer to this question. Please help... :-)

Challenge Task 1 of 1

Set the return value of the "check_speed" method to the string "safe" depending on the following condition: The speed passed in as an argument is at least 40. The speed passed in as an argument is less than or equal to 50. You should use the "&&" logical operator to accomplish this task.

ruby.rb
def check_speed(car_speed)
  if (car_speed > 40) && (car_speed <= 50)
    return "safe"
  else
    return "unsafe"
  end
end

check_speed(45)

Forgot to include the "bummer" message.

Bummer! The check_speed method isn't producing the correct output.

4 Answers

Charles Smith
Charles Smith
7,575 Points

Sorry you don't need the parenthesis. The problem is that It must check if it's >=40

You also don't need to call the function.

Ok >= 40 solved the problem. I missed the part "at least" which should include 40. Thanks Charles!

So you need to remove the first parentheses and make sure you close both the if statement and the define method. Here's how I solved it.

def check_speed(car_speed)
  if car_speed >= 40 && car_speed <= 50
    return "safe"
  end
end
Charles Smith
Charles Smith
7,575 Points

I think you need to wrap both conditions in parentheses.

if ( (car_speed > 40) && (car_speed <= 50) )

Thanks for the advice. Unfortunately it still isn't working. Here is another option I tried.

def check_speed(car_speed)
  if ((car_speed > 40) && (car_speed <= 50))
    return "safe"
  end
end

check_speed = check_speed(45)