mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-19 01:55:51 +08:00
11 lines
397 B
JavaScript
11 lines
397 B
JavaScript
const BinaryConvert = (number) => {
|
|
const result = []
|
|
let i
|
|
for (i = number; i > 0; i = parseInt(i / 2)) {
|
|
result.push(i % 2) // push the value (remainder)to array
|
|
} return Number(result.reverse().join(''))
|
|
// reverse index of array as string ,join and change the type of value to become Number
|
|
}
|
|
// call function and value as parameter to passing the value
|
|
export { BinaryConvert }
|