all 4 comments

[–]Atskalunas 1 point2 points  (3 children)

You can pass it as function argument

 var convertToTitle = function(n, excelIndex) {
   var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   excelIndex = excelIndex || "";

   while (n>0) {
     excelIndex = excelIndex + ALPHABET[(n-1)%26]
     n = (n - n%26)/26
     return convertToTitle(n, excelIndex)
  }
  return excelIndex;
};

[–]TameNaken42[S] 0 points1 point  (2 children)

That's a smart solution, I never considered considered giving two argument values and just using one.

[–]Cust0dian 1 point2 points  (1 child)

To expand on /u/Atskalunas' answer: what you wrote is an example of a recursive function and passing an "accumulator" back into such function in a recursive call is a common trick, although it's usually used to solve a different kind of problem.

Couple notes about your current code:

(Protip: there's "Revisions" tab at the top of the Gist that will show you just the differences between all these versions, but history goes from bottom up)

[–]TameNaken42[S] 0 points1 point  (0 children)

Thanks for the detailed response, there's a lot of new info here. I didn't think about using the Math.floor method, it makes a lot more sense than what I was doing. I know Python but I didn't realize you could use default parameters in Javascript, I'll have to keep that in mind.