4

I want a function that I can go from A to B, B to C, Z to A.

My function is currently like so:

function nextChar(c) {
    return String.fromCharCode(c.charCodeAt(0) + 1);
}
nextChar('a');

It works for A to X, but when I use Z... it goes to [ instead of A.

Rolando
  • 58,640
  • 98
  • 266
  • 407
  • 1
    You need to manually check for Z. You are increasing ASCII value here. –  Mar 29 '17 at 13:58
  • 1
    Can't you just check for 'A'? Just specify the end limit and wrap it back if it exceeds it. – Carcigenicate Mar 29 '17 at 13:58
  • @BibekSubedi Actually, it's the UTF-16 code unit value not the ASCII value. – Tom Blodget Mar 29 '17 at 16:34
  • I notice in your description you refer to 'A' to 'Z'. But in your example, you use 'a'. Those are different letter characters (as are 'ā', 'ą', …). JavaScript doesn't have the built-in concept of Letter but Unicode does and all characters in JavaScript are Unicode. You can build a regular expression for all letters [here](http://apps.timwhitlock.info/js/regex#). – Tom Blodget Mar 29 '17 at 16:38

4 Answers4

5

Simple condition.

function nextChar(c) {
    var res = c == 'z' ? 'a' : c == 'Z' ? 'A' : String.fromCharCode(c.charCodeAt(0) + 1);
    console.log(res);
}
nextChar('Z');
nextChar('z');
nextChar('a');
kind user
  • 40,029
  • 7
  • 67
  • 77
5

You could use parseInt with radix 36 and the opposite method Number#toString with the same radix, and a correction for the value.

function nextChar(c) {
    var i = (parseInt(c, 36) + 1 ) % 36;
    return (!i * 10 + i).toString(36);
}

console.log(nextChar('a'));
console.log(nextChar('z'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • @Gary, basically it checks the value of `i` and you get either with zero: `0 -> 1 * 10 + 0 => 10` or with eg `20`: `20 -> 0 * 10 + 20 => 20`. or in short take the value if it is not zero or 10, if it is zero. the value of `10` is the letter `'a'`. the value of `'z'` is `35`. by adding one and taking the remainder, you get zero. from the value zero, you need to get `'a'`. that's why you need the value of `10`. (`!` is a logical NOT operator and returns either `true` or `false`. by multiplication the value is casted to number, like `0` or `1`.) – Nina Scholz Sep 10 '17 at 14:42
3

function nextLetter(s){
    return s.replace(/([a-zA-Z])[^a-zA-Z]*$/, function(a){
        var c= a.charCodeAt(0);
        switch(c){
            case 90: return 'A';
            case 122: return 'a';
            default: return String.fromCharCode(++c);
        }
    });
}

console.log("nextLetter('z'): ", nextLetter('z'));

console.log("nextLetter('Z'): ", nextLetter('Z'));

console.log("nextLetter('x'): ", nextLetter('x'));

Reference

Community
  • 1
  • 1
Waqas
  • 171
  • 2
  • 6
2
function nextChar(c) {
        return String.fromCharCode(((c.charCodeAt(0) + 1 - 65) % 25) + 65);
}

where 65 stands for offset from 0 in ASCII table and 25 means that after 25th character it will start from the beginning (offset character code is divided by 25 and you get remainder that is offset back to proper ASCII code)

Markus Dresch
  • 5,290
  • 3
  • 20
  • 40