Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function convertNumberToId (number) {
    var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
    if (number < alphabet.length) {
        return alphabet[number];
    } else {
        return (
            convertNumberToId(Math.floor(number / alphabet.length) - 1)
            +
            convertNumberToId(number % alphabet.length)
        );
    }
}
function convertIdToNumber(id){
    var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
    if(id.length == 1){
        return alphabet.indexOf(id) >= 0 ? alphabet.indexOf(id) + 1 : -1;
    }
    else if (id.length > 0){
        let t = 0;
        for (let i = 0; i < id.length; i++) {
            t += convertIdToNumber(id.charAt(id.length - 1 - i)) * Math.pow(alphabet.length, i);
        }
        t--;
        return t;
    } else {
        return -1;
    }
}
function expectConversion(number) {
    var string = convertNumberToId(number),
        actualNumber = convertIdToNumber(string);
    if (actualNumber === number) {
        console.log('✔ Expects\t' + number + ' ==> ' + string);
    } else {
        console.warn('❌ Expected\t' + number + ' ==> ' + string + '\tActual\t' + actualNumber + ' <== ' + string);
    }
}
for(var i = 1; i <= 50; i++){
    expectConversion(i);
}
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers