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

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,558 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!!