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

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.