add binary convert (#521)

* add  binary convert
This commit is contained in:
Arwy Syahputra Siregar
2020-10-31 21:52:50 +07:00
committed by GitHub
parent 2af75f62c8
commit eab57b880a
3 changed files with 1983 additions and 398 deletions

10
Maths/BinaryConvert.js Normal file
View 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 }

View 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)
})
})

2361
package-lock.json generated

File diff suppressed because it is too large Load Diff