Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    very simple concatenation question

    Hi all,

    I really can't figure out what I'm doing wrong in a
    simple javascript block. I want to parse the letters of a word one by one,
    modify each letter, then output the modified set of letters.

    Code:
    ... <snip> ...
        lower = "hi"
        for (i=0;i<=strLen-1;i++)
        currChar = lower.charCodeAt(i);
        {
            resultChar = currChar + 1;
            strOutput = String.fromCharCode(resultChar);
            totOutput = totOutput + strOutput;
        }
        displayMessage (totOutput);
    any ideas why totOutput only ever shows the very last modified letter
    of the string (in this case "j" is the output, when I'd expect it to
    be "ij")?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    your for loop only increments i.
    the block enclosed with braces ({}) is not a loop, and runs only once,
    after i has been incremented to strLen.

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your code should be:

    Code:
    ... <snip> ...
        lower = "hi"
        for (i=0;i<=strLen-1;i++)
        {
           currChar = lower.charCodeAt(i);
           {
            resultChar = currChar + 1;
            strOutput = String.fromCharCode(resultChar);
            totOutput = totOutput + strOutput;
           }
        }
        displayMessage (totOutput);

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You all rock, thanks so much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •