Skip to content

Commit eab57b8

Browse files
authored
add binary convert (TheAlgorithms#521)
* add binary convert
1 parent 2af75f6 commit eab57b8

File tree

3 files changed

+2116
-531
lines changed

3 files changed

+2116
-531
lines changed

Maths/BinaryConvert.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const BinaryConvert = (number) => {
2+
const result = []
3+
let i
4+
for (i = number; i > 0; i = parseInt(i / 2)) {
5+
result.push(i % 2) // push the value (remainder)to array
6+
} return Number(result.reverse().join(''))
7+
// reverse index of array as string ,join and change the type of value to become Number
8+
}
9+
// call function and value as parameter to passing the value
10+
export { BinaryConvert }

Maths/test/BInaryConvert.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { BinaryConvert } from '../BinaryConvert'
2+
3+
describe('Binary Convert', () => {
4+
it('should return the correct value', () => {
5+
expect(BinaryConvert(12)).toBe(1100)
6+
})
7+
it('should return the correct value of the sum from two number', () => {
8+
expect(BinaryConvert(12 + 2)).toBe(1110)
9+
})
10+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy