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

Dino Tudor
Dino Tudor
1,767 Points

The And (&&) operator. Help solving quiz please.

What am i doing wrong? The first "if" statement doesn't set the correct value to return "safe" as asked. Please help me! Thanks!

ruby.rb
def check_speed(car_speed)
  if (car_speed >= 40) && (car_speed <= 50)
    return safe
  elsif (car_speed <= 39) && (car_speed >= 51)
    return unsafe
  end
end

1 Answer

andren
andren
28,521 Points

safe and unsafe are strings so you need to wrap them in quotes. In addition to that the conditions for your elseif is wrong. You are checking if car_speed is less than or equal to 39 AND more than or equal to 51. A number can't be both less than 39 and greater than 51 at the same time, so that statement will never be true.

You don't actually need any conditions for the last statement though, it should run for any value that does not fit within the if conditions, so you can just use an else statement like this:

def check_speed(car_speed)
  if (car_speed >= 40) && (car_speed <= 50)
    return "safe"
  else # else automatically runs if the above if statement does not run
    return "unsafe"
  end
end
Dino Tudor
Dino Tudor
1,767 Points

Thanks Andren! Got it!!