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

King Penson II
King Penson II
16,413 Points

I'm having trouble understanding what Challenge #1 in Logical Operators is asking...

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.

ruby.rb
def check_speed(car_speed)
  # write your code here
  if (car_speed == 40) && (car_speed <= 50)
    puts "safe"
end

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hello, King, There're several problems with your code here.

  1. Like Ryan has pointed out, your code didn't correctly check The speed passed in as an argument is at least 40, car_speed == 40 isn't good enough.
  2. missed a closing end for the if conditional.
  3. puts "safe" will only print out the "safe", the challenge is asking your method to return "safe" instead, so don't use the put keyword here.
def check_speed(car_speed)
  # write your code here
  if (car_speed >= 40) && (car_speed <= 50)
    "safe"
  end
end

hope that helps.

King Penson II
King Penson II
16,413 Points

I'm so used to closing with parenthesis and semi-colons, thank you for your help

Kevin Tinkler
Kevin Tinkler
2,416 Points

Also, 'setting the return value' of the method means instead of

puts "safe"

do

return "safe"

We call a method to get a value back. Using 'puts' will print out the word "safe", but does not make it available to whatever called the method.

Kevin Tinkler
Kevin Tinkler
2,416 Points

Which William pointed out while I was typing that! :)

William Li
William Li
Courses Plus Student 26,868 Points

upvoted! we can never have too many helpful answers :)

Kevin Naegele
Kevin Naegele
10,868 Points

This comment here helped this make sense! thanks

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

You'll need to test if the value passed to the function (car_speed) is greater than or equal to (>=) 40 AND (&&) less than or equal to (<=) fifty. With the function you have, it will only evaluate as true if the speed is exactly equal (==) to 40 since that number is the only one that passes both conditions.

King Penson II
King Penson II
16,413 Points

Thank you, I usually don't have trouble with these challenges. I keep reading the questions wrong