I would like to create a general algorithm for the sequence (I am not storing the sequence anywhere).. if
1=A
2=B
3=C
.
.
.
26=Z
27=AA
28=AB
29=AC
30=AD
What should be the code to get the value of any random number ? e.g 333
I would like to create a general algorithm for the sequence (I am not storing the sequence anywhere).. if
1=A
2=B
3=C
.
.
.
26=Z
27=AA
28=AB
29=AC
30=AD
What should be the code to get the value of any random number ? e.g 333
A brilliant example here HERE ..
function getCharCode(displayNumber){
var ordA = 'A'.charCodeAt(0);
var ordZ = 'Z'.charCodeAt(0);
var len = ordZ - ordA + 1;
var returnSequence = "";
while(displayNumber >= 0) {
returnSequence = String.fromCharCode(displayNumber % len + ordA) + returnSequence; // (displayNumber % 26 + 65) +""
displayNumber = Math.floor(displayNumber / len) - 1; //(displayNumber / 26) - 1
}
return returnSequence;
}
OR I modified this to ..
function getCharCode(displayNumber){
var returnSequence = "";
while(displayNumber >= 0) {
returnSequence = String.fromCharCode(displayNumber % 26 + 65) + returnSequence;
displayNumber = Math.floor(displayNumber / 26) - 1;
}
return returnSequence;
}
Try
var arr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
, i = -1
, n = -1
, len = 0
, max = arr.length + arr.length * arr.length
, res = {};
do {
if (n === arr.length -1) {
n = -1;
++i;
};
if (len < arr.length * 2 - 1) {
res[++len] = len < arr.length + 1 ? arr[++n] : res[1] + arr[++n];
} else {
res[++len] = arr[i] + arr[++n]
};
} while (len < max);
console.log("333:" + res[333], res);