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

Don't understand this Ruby coding exercise!! Please help.

https://pine.fm/LearnToProgram/chap_08.html

This link is to the lesson I'm having trouble with. If you scroll down to the part where it says One More Big Example. This is where I'm struggling. This might be more of a math problem than a code problem. I just don't understand how it works and I really want to. I understand what is happening in the code up until the line that says: left = left - write*100

This makes no sense to me because no matter what number is passed into the method, left is going to be set equal to 0.

For example, if I pass in 35, and write is then then set to 35/100, and then left is set to left - write*100. This just means 35 - .35*100. Which, of course, is equal to 0. Then it goes on to set write = to left(now 0)/10 which of course is 0 as well because 0 divided by anything is 0.

If you don't understand my question please ask for clarification and I'll do my best! Any help is incredibly appreciated.

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

35/100 equals 0, because you divided two integers and this did not get cast into a float (0.35 would be a float). So this will only equal more than 0 if you have at least 100 as your input. 150/100 will give you 1, same for 199/100. Try this in IRB. If, however, at least one of these numbers is a float (e.g. 35.0/100 or 35/100.0), then the output is cast as a float and you'd get 0.35. Hope this helps.

Yes- I figured that out and I will definitely give you the best answer. Thank you. I'm stuck somewhere else now though if you would please please please care to take a look. During the recursion (when it sets hundreds = englishNumber write) I can't fully grasp what is happening. So if we take 855 as an example. 855 is stored in left, and then write is set to 855/100 which = 8. Then left = 855 - 800(write x 100) which gives you 55. Then if write is greater than 0, which it is because it equals 8, then set hundreds to englishNumber write(which is 8). So, then 8 goes through the entire method and finally gets to if write > 0, then numString = numString + onesPlace[write-1] which allows it to retrieve the "eight" to put in front of " hundred". That's what I think is happening but please correct me if I'm wrong. So, what I don't get is, when that recursion begins, doesn't the code lose the 55 that was stored in left when it sets left = 8??? The code works, but how does it remember the 55 even after resetting the variable to 8??? I really hope this is clear and if you can give me a good answer I'll make you a roast beef sandwich and rub your feet if we ever meet.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Wait, it works for 855? I though it only does numbers from 0 to 100.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Ah, ok, I looked at the wrong code snippet, sorry :)

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

But left never equals 8. Left is always what is left after taking out hundreds, tens and then ones. write is the one that becomes 8 at first.

Yes- but when you do the recursion, that sets hundreds to englishNumber write (which, like you said, is 8). So then the whole method starts from the top using write(8) as number. Then it says left = number. Meaning, left = 8. Meaning left no longer = 55. Do you follow?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, I get it. It's the question of scoping. These are all local variables, so when recursion occurs, each iteration will have its own local variables with these names and they will not overlap. Once it's finished with englishNumber write, it forgets all the stuff that happened there and the variables are back to their original values. So I guess you need to read up on variable scoping in Ruby and experiment a bit with it in the console.

Thank you so much my friend. Really good answers. It's people like you that make Treehouse- Treehouse.