hi i was trying to get the if else function to work the if and end function turn yellow but the else function just stays black. when i run the program and enter the right number it still gives me the else alert. im going through the tutorial where you use the if and else function. I copied word for word the example but it doesnt work can someone help me
Thanks for the question - I think you are talking about Lesson 3:9 (Basic Ruby - Basic Flow Control) right?
Don't worry so much about the colouring of the
else- mine did the same (yellow on
end) but it works. The colouring is a handy guide but not critical to the program.
if ... else ... endprogram is like this:
number = 2 if number == 1 alert "Yes!" else alert "No!" end
... and I typed that into the HH editor (without Shoes.app around it) and it Run's fine. Changing the first line shows the different 'Yes' and 'No' appropriately.
Some tips if you are still having problems - ensure the
ifline includes the double-equals
==as in Ruby that is the comparison (is number the same as 2?), while in the first line, the single equals is assignment (variable number is now got the value 2)
Another thing is that sometimes ruby will compare things too well - such as the number 2 is not the same as the string '2', so watch for that. (eg:
number = '2'is a string and much be compared to a string
if number == '2'). This comes up in the number guessing game and needs to have
.to_sincluded to convert numbers to strings to they compare properly.
Feel free to paste in your code and we'll try to help.