...

View Full Version : very simple concatenation question



disquette
08-28-2008, 04:37 AM
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.



... <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")?

mrhoo
08-28-2008, 05:24 AM
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.

Pramit
08-28-2008, 05:28 AM
Your code should be:



... <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);

disquette
08-28-2008, 05:47 AM
You all rock, thanks so much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum