Created
November 14, 2015 18:28
-
-
Save wkronemeijer/f95e1769e172a1795d7e to your computer and use it in GitHub Desktop.
Turning a column name into a number for indexing in the range [1, →)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseHeader(letters: string): number { | |
function letterValue(letter: string): number { | |
return parseInt(letter, 36) - 9 | |
} | |
let result = 0 | |
let factor = 1 | |
if (letters.length === 1) { | |
result = letterValue(letters[0]) | |
} else { | |
for (let i = letters.length - 1; i >= 0; i -= 1) { | |
result += factor * letterValue(letters[i]) | |
factor *= 26 | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment