Hello fellow nerds. This should be basic, I hope. I have dabbled in coding in the past so I kind of think I know what I'm doing, but I don't apparently. I know only basic javascript (for/do/while loops, arrays, functions, etc). I was messing around and trying to write a little program that takes a piece of text and draws letters from it in random groups of 1-7 and then spits them into a text string with spaces in between. Just for fun. Or art, whatever. So, I got it to work (although my coding isn't exactly... elegant). However my problem is, the output always has too many spaces. Here is a link to my project. http://labs.codecademy.com/hEB/1#:workspace I made comments so you can see what's happening. Basically there is an if/else imbedded in a while loop, and the else statement (containing the code to make a space) seems to sometimes run more than once. I can't for the life of me figure out why it would run more than once, but it does consistently. Anyone?
I am no expert, but I did this: Added in output += prog; after the typeto in the roll function to see the length of the words. Got something like this: 1th3 san2 am6 eofate5 boutj5 scrip1 d1 i0 4 tget0 3 amn1 i6 tostop3 ing3 ext4 pace0 4 Notice that sometimes ONLY the length of the word (prog) was returned. And notice that when prog = 0, you get no letters in the next word. This creates a situation where there are back-to-back spaces. Tighten up roll so you can't return 0 and I THINK you'll have it Ed O.
Yeah, that was actually it. I asked a javascript forum and I was told to make it so typeTo rolls from 1-7, not 0-7. That fixed it, but I'm still new programming (and my brain isn't exactly logic-based) so I still fail to see why it caused what it caused. Perhaps it will dawn on me as I progress. Thanks man!
Not to derail, I don't know java, I've only ever written code for C, C#, C++ and python, but I really hope HCP reads this thread ...
Happy to help, although it sounds like you woulda figured it out elsewhere. Maybe I can restate what's happening in a way that helps you grok it. After each "word", it puts a space, right? This works well whenever there is a word longer than 0 characters. 12 345 6 7 890 Substituting "X" for " ", it looks like this: 12X345X6X7X890 The word lengths are 2, 3, 1, 1 and 3. Let's change the second-to-last word length to 0. We are basically removing the "7" from that sequence: 12X345X6XX890 By removing the 7, we have back-to-back Xs because there IS a word accounted for, but no letters from that word print, so it adds another X (" ")... Does this help? Make sense? Ed O.