-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution0058.js
51 lines (35 loc) · 1.47 KB
/
solution0058.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
--------------- 7 Kyu - Ones and Zeros ------------------
Instructions:
Given an array of ones and zeroes, convert the equivalent binary value to an integer.
Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.
Examples:
Testing: [0, 0, 0, 1] ==> 1
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 0, 1] ==> 5
Testing: [1, 0, 0, 1] ==> 9
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 1, 0] ==> 6
Testing: [1, 1, 1, 1] ==> 15
Testing: [1, 0, 1, 1] ==> 11
However, the arrays can have varying lengths, not just limited to 4.
-------------
Sample Tests
describe("One's and Zero's", () => {
it("Example tests", () => {
Test.assertEquals(binaryArrayToNumber([0,0,0,1]), 1);
Test.assertEquals(binaryArrayToNumber([0,0,1,0]), 2);
Test.assertEquals(binaryArrayToNumber([1,1,1,1]), 15);
Test.assertEquals(binaryArrayToNumber([0,1,1,0]), 6);
});
});
--------------
Psuedo Code:
-you can convert an integer to binary by dividing the integer by 2, and dividing the quotient of each division by two, until you reach zero. Put the remainders of each division in reverse order and you have the binary equivalent.
-However, we need to do the reverse of this, which is a bit more complicated
-Fortunately you don't need to to this manually, because parseInt() can take a second parameter that alters the base
-since binary format is base 2, you can simply use parseInt('#', 2)
*/
const binaryArrayToNumber = arr => {
return parseInt(arr.join(''), 2);
};