最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Elegant way to set alphabet variable like Excel columns - Stack Overflow

matteradmin7PV0评论

I'm looking for a nice way to have a variable alpha that would increment as follow: when x=0 -> alpha = "A"; x=1 -> alpha = "B"; ... x=25 -> alpha = "Z"; x=26 -> alpha = "AA"; x=27 -> alpha = "AB"

I'm looking for a nice way to have a variable alpha that would increment as follow: when x=0 -> alpha = "A"; x=1 -> alpha = "B"; ... x=25 -> alpha = "Z"; x=26 -> alpha = "AA"; x=27 -> alpha = "AB"

Share Improve this question edited Jan 13, 2017 at 9:48 Nate asked Jan 12, 2017 at 11:08 NateNate 7,88623 gold badges76 silver badges129 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You could use toString with base 36 for converting to the wanted letters.

function convert(n) {
    var result = '';
    do {
        result = (n % 26 + 10).toString(36) + result;
        n = Math.floor(n / 26) - 1;
    } while (n >= 0)
    return result.toUpperCase();
}

//           A  B   Z  AA  AB   CZ   DXH
console.log([0, 1, 25, 26, 27, 103, 3335].map(convert));
.as-console-wrapper { max-height: 100% !important; top: 0; }

EDITED

OK sorry I just didn't understand your need at first. Here is a working code.

Try this:

var x = 807;
var alpha = '';
var alphabetLength = 26;


var y = x.toString(alphabetLength);
chars = y.split('');
for (var i=0; i < chars.length; i++ ) {
	var charFactor = 65;
	var curChar = chars[i];
	if (isNaN(curChar)) {
	
		alpha += String.fromCharCode(curChar.toUpperCase().charCodeAt() + 10);
	} else {
		if ( i < chars.length-1) {
			charFactor--;
		}
		alpha += String.fromCharCode(parseInt(curChar) + charFactor);
	}
}

console.log(alpha)

Use String.fromCharCode method and generate string using char code.

// initialize as empty string
var alpha = '';
// iterate upto value is reaching -1 since index starts from 0
while (x > -1) {
  // get charactor by finding remainder 
  // and concatenate it with alpha
  alpha = String.fromCharCode(65 + x % 26) + alpha;
  // update value of variable x 
  // decrement by 1 since index starts from 0
  x = Math.floor(x / 26) - 1;
}

[1, 28, 25, 26, 27, 3335, 12, 10, 3, 6, 0].forEach(function(x) {
  var alpha = '';
  while (x > -1) {
    alpha = String.fromCharCode(65 + x % 26) + alpha;
    x = Math.floor(x / 26) - 1;
  }
  console.log(alpha);
});

I ended up with a one liner for this:

(alpha).toString(26).split('').map((b26) => (parseInt(b26,26) + 10).toString(36)).join('').toUpperCase()

The basic idea is to convert your number into base 26, which means 0-9a-p, and then add 10 so that the output range is a-z.

Here's a quick test showing that it works for the first 100 numbers.

for (let i = 0; i < 100; i++) {
  console.log(i, (i).toString(26).split('').map((b26) => (parseInt(b26,26) + 10).toString(36)).join('').toUpperCase())    
}
Post a comment

comment list (0)

  1. No comments so far