...

/

Solution Review: Convert Decimal Number to Binary Number

Solution Review: Convert Decimal Number to Binary Number

This review provides a detailed analysis of the solution to convert a decimal number to a binary number.

We'll cover the following...

Solution: Using Recursion

Press + to interact
function decimalToBinary(testVariable) {
// Base case
if (testVariable <= 1) {
return String(testVariable)
}
// Recursive case
else {
return decimalToBinary(Math.floor(testVariable / 2)) + decimalToBinary(testVariable % 2)
}
}
// Driver Code
var testVariable = 11
console.log(decimalToBinary(testVariable))

Explanation:

The simple method for converting a decimal number to a binary number is to keep track of the remainderremainder and the leftover dividenddividend ...

Access this course and 1400+ top-rated courses and projects.