JavaScript: Excel column title related with a column number
JavaScript Math: Exercise-97 with Solution
Write a JavaScript program to retrieve the Excel column title associated with a given column number (integer value).
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
Test Data:
(3) -> C
(27) -> AA
( 151) -> EU
Sample Solution-1:
JavaScript Code:
/**
* Function to convert a column number to its corresponding Excel column title.
* @param {number} n - The column number.
* @returns {string} - The Excel column title.
*/
function test(n) {
s = 1; // Start index for column titles (A = 1)
e = 26; // End index for column titles (Z = 26)
result = ""; // Initialize the result variable to store the column title
while ((n -= s) >= 0) { // Loop until the column number is greater than or equal to the start index
result = String.fromCharCode(parseInt((n % e) / s) + 65) + result; // Calculate the character for the current digit and prepend it to the result
s = e; // Update the start index for the next digit
e *= 26; // Update the end index for the next digit
}
return result; // Return the Excel column title
}
// Test cases
let n = 4;
console.log("n = " + n);
console.log("Excel column title related with the said column number: " + test(n));
n = 27;
console.log("n = " + n);
console.log("Excel column title related with the said column number: " + test(n));
n = 151;
console.log("n = " + n);
console.log("Excel column title related with the said column number: " + test(n));
Output:
n = 4 Excel column title related with the said column number: D n = 27 Excel column title related with the said column number: AA n = 151 Excel column title related with the said column number: EU
Flowchart:
Live Demo:
See the Pen javascript-math-exercise-97 by w3resource (@w3resource) on CodePen.
Sample Solution-2:
JavaScript Code:
/**
* Function to convert a column number to its corresponding Excel column title.
* @param {number} n - The column number.
* @returns {string} - The Excel column title.
*/
function test(n) {
result = ''; // Initialize the result variable to store the Excel column title
while(n > 0){ // Loop until n becomes 0
el = String.fromCharCode(65 + (n-1) % 26); // Calculate the character corresponding to the current digit
result = el + result; // Prepend the character to the result
n = ~~((n-1)/26); // Update n to get the next digit
}
return result; // Return the Excel column title
}
// Test cases
n = 4;
console.log("n = " + n);
console.log("Excel column title related with the said column number: " + test(n));
n = 27;
console.log("n = " + n);
console.log("Excel column title related with the said column number: " + test(n));
n = 151;
console.log("n = " + n);
console.log("Excel column title related with the said column number: " + test(n));
Output:
n = 4 Excel column title related with the said column number: D n = 27 Excel column title related with the said column number: AA n = 151 Excel column title related with the said column number: EU
Flowchart:
Live Demo:
See the Pen javascript-math-exercise-97-1 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus.
Previous: Number of trailing zeroes in a factorial.
Next: Column number that relates to a column title.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/javascript-exercises/javascript-math-exercise-97.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics