Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript alphabet to number

/**
 * Converts a spreadsheet column letter to a number. eg. A -> 1
 * 
 * @param {String} str - (required) The spreadsheet column letter to convert to a number.
 * 
 * @returns {(Number|undefined)} The column number or undefined.
 */
function convertLetterToNumber(str) {
  if ((typeof str === "string" || str instanceof String) && /^[a-zA-Z]+$/.test(str)) {
    str = str.toUpperCase();
    let out = 0,
      len = str.length;
    for (pos = 0; pos < len; pos++) {
      out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
    }
    return out;
  } else {
    return undefined;
  }
}

convertLetterToNumber("A"); // 1
convertLetterToNumber("b"); // 2
convertLetterToNumber("Ba"); // 53
convertLetterToNumber("dE"); // 109
convertLetterToNumber("ZZZZ"); // 475254

convertLetterToNumber("A"); // 1
convertLetterToNumber("b"); // 2
convertLetterToNumber("Ba"); // 53
convertLetterToNumber("dE"); // 109
convertLetterToNumber("ZZZZ"); // 475254
Comment

js int to alphabet

var value = 10;

document.write((value + 9).toString(36).toUpperCase());
Comment

alphabet to number javascript

str = 'z'
n = (str == str.toLowerCase()) ? 96 : 64
result = str.charCodeAt(0) - n
console.log(result);
Comment

PREVIOUS NEXT
Code Example
Javascript :: Uncaught (in promise): NullInjectorError 
Javascript :: string replace in javascript 
Javascript :: csrf token method 
Javascript :: string methods javascript count number of words inside a string 
Javascript :: console.time in javascript 
Javascript :: copyright js 
Javascript :: laravel query json 
Javascript :: pymongo add json to collection 
Javascript :: how to get user input in javascript 
Javascript :: jquery search for string in text 
Javascript :: vscode regex replace only group 
Javascript :: transitionduration 
Javascript :: javascript decode uri 
Javascript :: test if property exists javascript 
Javascript :: run cypress 
Javascript :: react native new line character 
Javascript :: sort array of objects javascript by date 
Javascript :: neo4j create relationship between existing nodes 
Javascript :: react how to create range 
Javascript :: check if input is a number javascript 
Javascript :: does onclick work for textboxes javascript 
Javascript :: dynamically add script code to page 
Javascript :: javascript line through 
Javascript :: cypress click link contains text 
Javascript :: jquery form validation plugin callback function 
Javascript :: putting a loop into an array javascript 
Javascript :: javascript merge two objects 
Javascript :: Convert number to array of digits js 
Javascript :: cannot use import statement outside a module in jest 
Javascript :: angular dynamic class 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =