Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Next letter 1

Status
Not open for further replies.

SlykFX

Programmer
Oct 12, 2002
76
GB
is it possible to increase the letter that a variable holds?

e.g.
letter = 'a'
-some code-
letter value = b

i want to repeat that process for an non-predetermined amount
i.e.
some times it will stop at b, other it will stop at q depending on the number of values being processed

i know i can do it with numbers by using ++ but i also need to use letters in conjunction with numbers

the final output from the script would be something like
1a
1b
1c
1d
2a
3a
3b
etc.

however i cant hard code this because other times it may be
1a
1b
2a
2b
2c
3a
4a
etc.
it all depends on the number of values in the array being processed

if you understand what i mean :)

anyway thats what i want to do if it is possible

thanks in advance for any help
it is much appreciated

I can't be bothered to have a sig!
 
Yup, letters are numbers. 'a' = 97, 'A' = 65, 'b' = 98 etc.

Just need to convert character to number, check for 90 (Z) or 122 (z), add 1 to number and convert back to character:
Code:
<script>
function nextLetter(letter){
 var letterCode = letter.charCodeAt(0);
 switch(letterCode){
  case 90: {
   letterCode = 65;
   break;
  }
  case 122: {
   letterCode = 97;
   break;
  }
  default : {
   letterCode++;
   break;
  }
 }
  return String.fromCharCode(letterCode);
}
</script>
 
function a(){
var letter = &quot;1a&quot;;
var n = letter.substr(1,1);
n = n.charCodeAt(0);alert(n);
n++;
n = String.fromCharCode(n);alert(n);

letter = letter.substr(0,1)+n;

alert(letter);
}
 
thanks guys
that really helps me :)

I can't be bothered to have a sig!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top