Any javascripters/coders wanna help me with something?

Discussion in 'Blazers OT Forum' started by TradeNurkicNow, Feb 28, 2013.

  1. TradeNurkicNow

    TradeNurkicNow piss

    Joined:
    Sep 16, 2008
    Messages:
    5,196
    Likes Received:
    676
    Trophy Points:
    113
    Occupation:
    hell
    Location:
    shit
    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?
     
  2. Ed O

    Ed O Administrator Staff Member Administrator

    Joined:
    Sep 15, 2008
    Messages:
    10,701
    Likes Received:
    2,826
    Trophy Points:
    113
    Location:
    Seattle, WA
    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.
     
  3. TradeNurkicNow

    TradeNurkicNow piss

    Joined:
    Sep 16, 2008
    Messages:
    5,196
    Likes Received:
    676
    Trophy Points:
    113
    Occupation:
    hell
    Location:
    shit
    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!
     
  4. Nikolokolus

    Nikolokolus There's always next year

    Joined:
    Sep 19, 2008
    Messages:
    30,704
    Likes Received:
    6,198
    Trophy Points:
    113
    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 ...
     
  5. drexlersdad

    drexlersdad SABAS

    Joined:
    Sep 16, 2008
    Messages:
    4,825
    Likes Received:
    255
    Trophy Points:
    83
    Location:
    NEW New Hampshire
    looks like some nazi code windtalker super secret spy shit

    i will have no part in this
     
  6. Ed O

    Ed O Administrator Staff Member Administrator

    Joined:
    Sep 15, 2008
    Messages:
    10,701
    Likes Received:
    2,826
    Trophy Points:
    113
    Location:
    Seattle, WA
    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.
     
  7. magnifier661

    magnifier661 B-A-N-A-N-A-S!

    Joined:
    Oct 2, 2009
    Messages:
    59,328
    Likes Received:
    5,588
    Trophy Points:
    113
    Occupation:
    Cracking fools in the skull
    Location:
    Lancaster, California
    Nerds!!!!!!
     
  8. TradeNurkicNow

    TradeNurkicNow piss

    Joined:
    Sep 16, 2008
    Messages:
    5,196
    Likes Received:
    676
    Trophy Points:
    113
    Occupation:
    hell
    Location:
    shit
    Oh. Ohhhhhhhhhhh. Shit that makes total sense. Stupid 0...

    Thanks a ton man. This helps me a lot.
     

Share This Page