Hello! I made a base converting program in c++ but I have a problem converting it to a JavaScript code as I am not that good in Javascript yet. Any help will be gladly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12
string to_base (int number,int base)
{
string bazes = "0123456789ABCDEF";
string result = " ";
do{
result = bases[ number%base ]+result;
number = number/base;
}while(number>0);
return result;
}
The difference is minimal as basically you only need to remove the typing.
1 2 3 4 5 6 7 8 9
function to_base(number, base) {
bases = "01234566789ABCDEF";
result = "";
do {
result += bases.charAt(number % base);
number /= base;
} while (number > 0);
return result;
}
You don't even have to make this function yourself when there is a parseInt(num, radix) that does exactly what your function wants to do.