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

Richard Poirier
PLUS
Richard Poirier
Courses Plus Student 2,930 Points

Stumped for the night

Evening to all!

I have been moving along through the next few sections and I am finally stuck on this code challenge...after several attempts, I am giving up for now. I understand how you make a variable equal to "true" and then use an "if" statement to show that variable's value if the conditions are met. I also completely understand the video showing if and else statements...but I am stumped on the following question "Set a variable called "too_fast" equal to "true" if the car_speed is faster than the speed_limit and "false" if the car_speed is less than the speed_limit." How do you set the SAME variable to be two different answers,,,I tried a few options but the code challenge didn't like any of them. I tried thinking about using different operators and such...but the answer isnt coming to me...(attaching one attempt of many for code) Thanks!

ruby.rb
car_speed = 55
speed_limit = 60
too_fast = true

if car_speed > speed_limit
  puts "too_fast"
else
  puts "false"
end

Although I can't answer this for you (I'm not as far along in Ruby as yourself!), I did want to commend you on knowing when to "throw in the towel" for the night, take care of yourself (by sleeping vs. obsessing over figuring this out), and reach out to others for help. I believe that's a great strategy in IT, and I'm not sure how much this strategy is used in the industry, but it's certainly helpful here! :)

2 Answers

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

Well, you're not setting the variable to two different values at the same time. Mark Ihrig has a good explanation there of how the if condition works. Every time you say x = 7 or x = 10 or x = true in your code you're overwriting the current value of x with the new value you're giving it. But the real problem with this challenge is that you're printing the results, which isn't what they asked you to do. Take a look:

car_speed = 55
speed_limit = 60
too_fast = true

if car_speed > speed_limit
  too_fast = true
else
  too_fast = false
end

Variables are just that... variable. Their values can and very likely will change during the course of the program. Just because a car is going 50 now, doesn't mean it'll go 50 when my dad with the lead foot drives it. I promise you! But here we start with the too_fast set to true. But if we recheck the car speed and the car isn't going that fast anymore we need to be able to set the too_fast to false. That means that we can do multiple checks on the car speed throughout our program. Hope that makes sense!

Richard Poirier
Richard Poirier
Courses Plus Student 2,930 Points

Thanks so much for the followup! I appreciate the explanations. I think that is the factor I wasn't getting...I was thinking..how would you have too_fast equal 2 things at one...but if one overrides the other...I guess kinda like CSS overriding elements are you work down code...it is in fact variable and not set at a certain value. The videos didn't seem to really highlight this point...so how would you know you could so such a thing = D. Thanks again!

-Richard

The challenge is to set line 3 to the proper answer (true or false) Here is your code:

car_speed = 55 speed_limit = 60 too_fast = true

With this code, you are saying that the car is going 55, and the speed limit is 60, so in theory the car is not going too fast. On line 3, because you have "true" , you are saying that the car is indeed going too fast(over 60). The speed 55 is under the speed limit, so it wouldn't be going too fast. Line 3 should be true, only if the car speed is over the speed limit and set to false if 55 is under the speed limit. The answer on Line 3 is the reason you are not passing.

If the car speed on line 1 is 60 or under, too_fast=false (the car is at or below the speed limit) if the car speed on line 1 is 61 or over, too_fast=true (the car is over the speed limit)