-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathones_and_zeros.js
More file actions
27 lines (24 loc) · 1.73 KB
/
ones_and_zeros.js
File metadata and controls
27 lines (24 loc) · 1.73 KB
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
/******************************************************************************************
* CODEWARS ONES AND ZEROS CHALLENGE *
* *
* Problem Statement *
* 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 *
* Input 1: [0, 0, 0, 1] *
* Output 1: 1 *
* *
* Input 2: [1, 0, 0, 1] *
* Output 2: 9 *
* *
* Input 3: [1, 1, 1, 1] *
* Output 3: 15 *
*****************************************************************************************/
const binaryArrayToNumber = (arr) => {
let binarySum = 0;
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] === 1) binarySum += Math.pow(2, arr.length - 1 - i);
}
return binarySum;
};