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

help

Using an "elsif" statement, modify the code below to check whether or not the car_speed is equal to the speed_limit. If it is, set the variable "going_speed_limit" to true.

http://teamtreehouse.com/library/ruby-operators-and-control-structures/ruby-control-structures/elsif-challenge

4 Answers

The trick is to check the equality between the number for car_speed and speed_limit using a second conditional statement with elsif

car_speed = 55
speed_limit = 55

if car_speed < speed_limit
  too_fast = false
elsif car_speed == speed_limit
  going_speed_limit = true
else
  too_fast = true
end
Elle Kasser
Elle Kasser
4,605 Points

I tried adding elsif after the else statement - that didn't work - I'm curious why?

I also added the elsif after the else and didn't understand why.

I'm not 100% sure, but I think the program would end when it finds out that if car_speed is more than (or equal to, in this case) speed_limit, it returns true for too_fast. Anything other than being equal or less means too_fast is true.

The else statement has won and so there is no need to go to elsif if it has decided that.

The reason for putting elsif first would be to check if the two variables are equal to each other before deciding it is too fast.

My issue was I was typing elseif not elsif.

Joseph Groark
Joseph Groark
24,499 Points

I also put the elsif after the else and it didn't work?? Anyone know why that wouldn't work?

Javier Pacheco
Javier Pacheco
3,418 Points

So elsif must come before else. I think that is correct.