Skip to content

Instantly share code, notes, and snippets.

@mcshaman
Created September 23, 2015 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcshaman/daf5bcd0918cf1a6ba27 to your computer and use it in GitHub Desktop.
Save mcshaman/daf5bcd0918cf1a6ba27 to your computer and use it in GitHub Desktop.
Javascript function that converts alphabetical styled ordered list letters to number
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