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 Ruby Control Structures Elsif challenge

Andrew Ackerman
Andrew Ackerman
8,347 Points

Help with elsif function

I'm not entirely sure what I'm doing wrong, I don't think I've quite got the hang of this elsif operator so any clarification and explanation would be appreciated!

ruby.rb
car_speed = 55
speed_limit = 55

if car_speed < speed_limit
  too_fast = false
else
  too_fast = true

elsif == speed_limit = car_speed
  going_speed_limit = true
end

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing pretty well here, but there's some misunderstanding with the difference between comparison operators, assignment operators, and conditional statements. Let's take a look:

car_speed = 55
speed_limit = 55

if car_speed < speed_limit  #if the car is going less than the speed limit
  too_fast = false
elsif car_speed == speed_limit. #otherwise if the car is going the speed limit
  going_speed_limit = true
else #if everything else is false...
  too_fast = true
end

The else should always be the last thing that happens. This says "if everything else fails do this". There are only three options here. The car is going less than the speed limit, the car is going the speed limit, or the car is speeding.

We use a single equals sign to assign a value to a variable. We use a double equals to ask if something is equal to something else. If you use a single equals, you aren't asking, you're telling it "you are now equivalent to this".

Hope this helps! :sparkles:

Andrew Ackerman
Andrew Ackerman
8,347 Points

Thank you so much for that explanation! Much clearer now!