Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decimal to binary conversion using recursion in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should use recursion to construct a string representing the binary notation of that number.
For example ?
f(4) = '100' f(1000) = '1111101000' f(8) = '1000'
How Binary Conversion Works
Binary conversion involves repeatedly dividing a number by 2 and collecting the remainders. The remainders, read in reverse order, form the binary representation.
Recursive Implementation
The recursive approach builds the binary string by processing remainders from the most significant bit to the least significant bit:
const decimalToBinary = (num) => {
if(num >= 1) {
// If num is not divisible by 2 then recursively return proceeding
// binary of the num minus 1, 1 is added for the leftover 1 num
if (num % 2) {
return decimalToBinary((num - 1) / 2) + 1;
} else {
// Recursively return proceeding binary digits
return decimalToBinary(num / 2) + 0;
}
} else {
// Exit condition
return '';
}
};
console.log(decimalToBinary(4));
console.log(decimalToBinary(1000));
console.log(decimalToBinary(8));
100 1111101000 1000
How the Recursion Works
Let's trace through the execution for decimalToBinary(4):
decimalToBinary(4)
?? 4 % 2 = 0, so: decimalToBinary(4/2) + 0 = decimalToBinary(2) + 0
?? 2 % 2 = 0, so: decimalToBinary(2/2) + 0 = decimalToBinary(1) + 0
?? 1 % 2 = 1, so: decimalToBinary((1-1)/2) + 1 = decimalToBinary(0) + 1
?? 0 < 1, so: return ''
?? Result: '' + 1 = '1'
?? Result: '1' + 0 = '10'
?? Result: '10' + 0 = '100'
Alternative Simplified Version
Here's a cleaner recursive implementation using the standard division approach:
const decimalToBinarySimple = (num) => {
if (num === 0) {
return '';
}
return decimalToBinarySimple(Math.floor(num / 2)) + (num % 2);
};
// Handle edge case for 0
const convertToBinary = (num) => {
return num === 0 ? '0' : decimalToBinarySimple(num);
};
console.log(convertToBinary(0));
console.log(convertToBinary(5));
console.log(convertToBinary(10));
0 101 1010
Comparison with Built-in Method
JavaScript provides a built-in method for binary conversion:
// Using built-in toString() method
console.log("Built-in method:");
console.log((4).toString(2));
console.log((1000).toString(2));
console.log((8).toString(2));
console.log("\nRecursive method:");
console.log(decimalToBinary(4));
console.log(decimalToBinary(1000));
console.log(decimalToBinary(8));
Built-in method: 100 1111101000 1000 Recursive method: 100 1111101000 1000
Conclusion
The recursive approach to decimal-to-binary conversion demonstrates how recursion can elegantly solve problems by breaking them into smaller subproblems. While the built-in toString(2) method is more practical, understanding the recursive implementation helps grasp both binary conversion and recursion concepts.
