I don't understand what this code is doing

#1
function numberToString(n, base) {
   var result = "", sign = "";
   if (n < 0) {
       sign = "-";
       n = -n;
   }
   do { 
       result = String(n % base) + result;
       n = Math.floor(n / base);
   } while (n > 0);
   return sign + result; 
    }
 console.log(numberToString(5,2));

Output: 101

The “Do” portion of the code I do not understand. Is this recursion? Is the computer running through the whole “Do” statement multiple times? Any help is greatly appreciated. Thank you.

0 Likes

#2

That is a do…while loop.

0 Likes

closed #3

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.

0 Likes