Given a decimal number A and a base B, convert it into its equivalent number in base B.
0 <= A <= 512
2 <= B <= 10
The first argument will be decimal number A.
The second argument will be base B.
Return the conversion of A in base B.
Input 1:
A = 4
B = 3
Input 2:
A = 4
B = 2
Output 1:
11
Output 2:
100
Explanation 1:
The decimal 10 in base 2 is 1010.
Explanation 2:
The decimal 8 in base 3 is 22.
function decimalToBase(A, B) {
if (A === 0) return '0';
let result = '';
while (A > 0) {
let remainder = A % B;
result = remainder + result;
A = Math.floor(A / B);
}
return result;
}
// Example usage:
console.log(decimalToBase(4, 3)); // Output: "11"
console.log(decimalToBase(4, 2)); // Output: "100"