-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathones-and-zeros.js
81 lines (69 loc) · 2.04 KB
/
ones-and-zeros.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function binaryArrayToNumber (arr) {
// place to store the sum
let sum = 0;
// iterate over the array
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
// take 2 raised to the power arr.length - 1 - i
// multiply by current value in array
// add to sum
const power = arr.length - 1 - i;
const powerOf2 = (2 ** power);
sum += powerOf2 * value;
}
// return the sum
return sum;
}
function binaryArrayToNumber (arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
sum += (2 ** (arr.length - 1 - i)) * value;
}
return sum;
}
function binaryArrayToNumber (arr) {
let sum = 0;
arr.forEach((value, i) => {
sum += (2 ** (arr.length - 1 - i)) * value;
});
return sum;
}
function binaryArrayToNumber (arr) {
return arr.reduce((sum, value, i) => {
return sum + (2 ** (arr.length - 1 - i)) * value;
}, 0);
}
function binaryArrayToNumber (arr) {
return arr.reduce((sum, value, i) => sum + (2 ** (arr.length - 1 - i)) * value, 0);
}
function binaryArrayToNumber (arr) {
return arr
.map((value, i) => (2 ** (arr.length - 1 - i)) * value)
.reduce((sum, value) => sum + value, 0);
}
function binaryArrayToNumber (arr) {
return arr.length ? (2 ** (arr.length - 1)) * arr[0] + binaryArrayToNumber(arr.slice(1)) : 0;
}
function binaryArrayToNumber (arr) {
if (arr.length) {
return (2 ** (arr.length - 1)) * arr[0] + binaryArrayToNumber(arr.slice(1));
}
return 0;
}
// skely!
function binaryArrayToNumber(arr) { return parseInt(arr.join(''),2); }
function binaryArrayToNumber (arr) {
return arr.reverse().reduce((sum, value, i) => sum + (2 ** i) * value, 0);
}
function binaryArrayToNumber (arr) {
return arr.reverse().reduceRight((sum, value, i) => sum + (2 ** i) * value, 0);
}
// 0001
// 2^3 * 0 + 2^2 * 0 + 2^1 * 0 + 2^0 * 1
// 0 + 0 + 0 + 1
// 1
console.log(binaryArrayToNumber([0,0,0,1]), 1);
console.log(binaryArrayToNumber([0,0,1,0]), 2);
console.log(binaryArrayToNumber([1,1,1,1]), 15);
console.log(binaryArrayToNumber([0,1,1,0]), 6);