Created
September 23, 2015 09:45
-
-
Save mcshaman/daf5bcd0918cf1a6ba27 to your computer and use it in GitHub Desktop.
Javascript function that converts alphabetical styled ordered list letters to number
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 letterToNumber( str ) { | |
var string = str.toUpperCase(), | |
length = string.length, | |
value, | |
number = 0; | |
// Validate string | |
if( ! string.match( /^[A-Z]+$/ ) ) { | |
throw new Error( 'String contains non-alphabetic characters' ); | |
} | |
for( i = 0; i < length; i++ ) { | |
value = string.charCodeAt( i ) - 64; | |
number += value * Math.pow( 26, length - i - 1 ); | |
} | |
return number; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment