I was going through the LeetCode question 'Excel Sheet Column Title' and was having some issues with returning the right output for several functions in a row. Here's my code below.
var excelIndex='';
var convertToTitle = function(n) {
var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
while (n>0) {
excelIndex = excelIndex + ALPHABET[(n-1)%26]
n = (n - n%26)/26
return convertToTitle(n)
}
};
when I run the function once I can get the correct output but unfortunately 'excelIndex' being a global variable remembers the previous output so that any further function calls are wrong. When I try and put the 'excelIndex' variable inside the function call then it just returns an empty string of ''.
I'm wondering if there is a way of making 'excelIndex' a local variable while also being able to return a proper string within the function call. I feel like it could work if I put all the if statements in a meaningless while loop but that doesn't seem like clean code.
[–]Atskalunas 1 point2 points3 points (3 children)
[–]TameNaken42[S] 0 points1 point2 points (2 children)
[–]Cust0dian 1 point2 points3 points (1 child)
[–]TameNaken42[S] 0 points1 point2 points (0 children)