mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00

committed by
GitHub

parent
2af75f62c8
commit
eab57b880a
10
Maths/BinaryConvert.js
Normal file
10
Maths/BinaryConvert.js
Normal file
@ -0,0 +1,10 @@
|
||||
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 }
|
10
Maths/test/BInaryConvert.test.js
Normal file
10
Maths/test/BInaryConvert.test.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { BinaryConvert } from '../BinaryConvert'
|
||||
|
||||
describe('Binary Convert', () => {
|
||||
it('should return the correct value', () => {
|
||||
expect(BinaryConvert(12)).toBe(1100)
|
||||
})
|
||||
it('should return the correct value of the sum from two number', () => {
|
||||
expect(BinaryConvert(12 + 2)).toBe(1110)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user