forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryConvert.js
More file actions
Latest commit
25 lines (21 loc) · 565 Bytes
/
Copy pathBinaryConvert.js
File metadata and controls
25 lines (21 loc) · 565 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* @function BinaryConvert
* @description Convert the decimal to binary.
* @param {Integer} num - The input integer
* @return {Integer} - Binary of num.
* @see [BinaryConvert](https://www.programiz.com/javascript/examples/decimal-binary)
* @example BinaryConvert(12) = 1100
* @example BinaryConvert(12 + 2) = 1110
*/
constBinaryConvert=(num)=>{
letpower=1
letbinary=0
while(num){
constrem=num%2
num=Math.floor(num/2)
binary=rem*power+binary
power*=10
}
returnbinary
}
export{BinaryConvert}