1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| function convert(num,scale){ let remainder = 0 let stack = new Stack() let string = '' let code = '0123456789ABCDEF' while(num>0){ remainder = num % scale stack.push(remainder) num = Math.floor(num/scale) } while(!stack.isEmpty()){ string += code[stack.pop()] } console.log(string) }
convert(138,16) convert(14,2)
|