Reputation: 229
I know how to do this in Php, as it is a simple, but i am struggling to get a version working.
for ($i = 'A'; $i < 'Z'; $i++) {
$k = $i;
$k++;
echo "$i->$k, ";
}
Basically i want to pass a letter into a function and get the next letter in the alphabet. Is there any easy ways or directions that i could be pointed in.
Upvotes: 0
Views: 4615
Reputation: 203
I had a similar issue, I modified the answer to this and it worked for me... from the fronted inside for loop just call getNextLetter(i);
typescript code:
getNextLetter(num): String {
var code: number = "abcdefghijklmnopqrstuvwxyz".charCodeAt(num);
return String.fromCharCode(code);
}
Upvotes: 0
Reputation: 10613
Here is a working code for you:
https://plnkr.co/edit/SP6LzMfLzeNniiteDmRQ?p=preview
var upper = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
var alphabet = {};
for(var j = 0; j < upper.length; j++)
alphabet[upper[j]] = j;
console.log(alphabet);
function NextLetter(l)
{
console.log('Current letter index:', alphabet[l]);
console.log('Next letter:',upper[alphabet[l]+1] );
}
//Test
NextLetter('C');
Upvotes: 1
Reputation: 23506
This should do what you want:
getNextLetter(char: String): String {
code: Number = char.charCodeAt(0);
code++;
return String.fromCharCode(code);
}
Upvotes: 1
Reputation: 1891
There are two nice JS functions that can help you out:
charCodeAt
- extracts the letter Unicode.fromCharCode
- parse Unicode to a letter.
var k, i = 'A'.charCodeAt();
var loopEnd = 'Z'.charCodeAt();
for (i; i < loopEnd; i++) {
k = i;
k++;
console.log(String.fromCharCode(i) + " -> " + String.fromCharCode(k));
}
Upvotes: 0