1 //进制转换(针对基数为2~9) 2 function mulBase(num, base) { 3 var s = new Array(); 4 do { 5 var least = num % base; 6 s.push(least); 7 num = (num - least) / base; 8 } while(num > 0); 9 var stringFy = '';10 while(s.length > 0) {11 stringFy += s.pop();12 }13 return stringFy;14 }
1 var num = 56;2 var base = 2;3 var newNum = mulBase(num, base);4 console.log(num + " converted to base " + base + " is " + newNum);
采用栈的思想