Skip to content

Instantly share code, notes, and snippets.

@PhilomathJ
Last active April 1, 2021 14:25
Show Gist options
  • Select an option

  • Save PhilomathJ/33f0aa7089aab06d8ba272e9d715a090 to your computer and use it in GitHub Desktop.

Select an option

Save PhilomathJ/33f0aa7089aab06d8ba272e9d715a090 to your computer and use it in GitHub Desktop.

Revisions

  1. Jeremy Fairbanks revised this gist Apr 1, 2021. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions columnIndexToLetter.ts
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,13 @@
    * Recursively returns the letter combination corresponding to the column for the given index
    * This is a more functional programming approach compared to the usual loop-based method
    * Tested to column index out to one decillion
    * @param {number} num Index to be converted
    * @returns {string} Letter combination as column indicator
    * @param {number} num Index to be converted
    * @returns {string} Letter combination as column indicator
    */
    const columnIndexToLetter = (num: number): string => {
    return num <= 26
    ? String.fromCharCode((num === 0 ? 26 : num) + 64)
    : columnIndexToLetter(Math.floor((num - 1) / 26)) + columnIndexToLetter(num % 26);
    return num <= 26
    ? String.fromCharCode((num === 0 ? 26 : num) + 64)
    : columnIndexToLetter(Math.floor((num - 1) / 26)) + columnIndexToLetter(num % 26);
    };

    // Testing
  2. Jeremy Fairbanks created this gist Apr 1, 2021.
    21 changes: 21 additions & 0 deletions columnIndexToLetter.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    /**
    * Recursively returns the letter combination corresponding to the column for the given index
    * This is a more functional programming approach compared to the usual loop-based method
    * Tested to column index out to one decillion
    * @param {number} num Index to be converted
    * @returns {string} Letter combination as column indicator
    */
    const columnIndexToLetter = (num: number): string => {
    return num <= 26
    ? String.fromCharCode((num === 0 ? 26 : num) + 64)
    : columnIndexToLetter(Math.floor((num - 1) / 26)) + columnIndexToLetter(num % 26);
    };

    // Testing
    const column: number = 28;

    console.log('column', column);

    const letter = columnIndexToLetter(column);

    console.log('letter', letter);