79103992

Date: 2024-10-19 00:41:17
Score: 0.5
Natty:
Report link

I view this more of a math problem than a coding problem.

I will just illustrate how I am thinking about the problem with a couple examples and then generalize.

Consider the number 438:

In order to compute the number, the formula is:

410^2 + 310^1 + 8*10^0, or
400 + 30 + 8 = 438.

(Remember that any number to the zeroth power is 1, and that the numbers cycle in powers of 10.)


Similarly, how do you compute BFX for example?

Note that there are 26 letters in the alphabet.

The formula is similar:

B26^2 + F26^1 + X*26^0

Now replace B with 2, F with 6, and X with 24:

You get:

226^2 + 626^1 + 24*26^0

1352 + 156 + 24 = 1532


With those 2 examples in mind, here is a function (columnNumber) that will convert 3 letters to a number in the convention of excel columns. It takes up to a 3 letter string and converts the value to a number.

Note, per makers of excel, the last column is 'XFD' as there are a maximum number of columns of 16,384. Other than making the input upper case and ensuring 3 characters, I have not done any type of validation of the input as to not distract from the essence of the function.

function columnNumber(str) {
  //note that strings are zero based so the 
  //indexed position of the letter in the string
  //will tell you the letters numerical value
  //So A's index is 1, Z's index is 26
  let alphabet = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  //first ensure the string is 3 characters long
  //I do this by adding to the beginning of the 
  //string then slicing off the last 3 characters.

  str = "___" + str;
  str = str.slice(-3);
  str = str.toUpperCase(); //just in case

  //get the left middle and right letter/character
  let leftChar = str.substring(0, 1);
  let middleChar = str.substring(1, 2);
  let rightChar = str.substring(2);

  //convert the 3 letters to corrresponding numbers
  let leftCharValue = alphabet.indexOf(leftChar);
  let middleCharValue = alphabet.indexOf(middleChar);
  let rightCharValue = alphabet.indexOf(rightChar);

  //compute the numericalValue
  let numericalValue = leftCharValue * 26 * 26 + middleCharValue * 26 + rightCharValue;
  return (numericalValue);
}

function calculate() {
  let str = document.getElementById("char-input").value;
  document.getElementById("result").innerHTML = columnNumber(str);
}

document.getElementById("calc-button").addEventListener("click", calculate);
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Column String Heading Calculator</title>
  <style>
    input,
    button {
      display: block;
    }
  </style>
</head>

<body>
  <input id="char-input" type="text" value="XFD">
  <button id="calc-button">Calculate</button>
  <div id="result"></div>
  <script src="columnCalculator.js"></script>
</body>

</html>

Reasons:
  • Blacklisted phrase (1): how do you
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: garydavenport73