I don't understand what this code is doing

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.

That is a do…while loop.

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