Skip to content

Commit 44b34af

Browse files
authored
Create Array2Binary addition
1 parent 3619bad commit 44b34af

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Array2Binary addition

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*Description:
2+
Given an array containing only integers, add all the elements and return the binary equivalent of that sum.
3+
4+
If the array contains any non-integer element (e.g. an object, a float, a string and so on), return false.
5+
6+
Note: The sum of an empty array is zero.
7+
8+
arr2bin([1,2]) == '11'
9+
arr2bin([1,2,'a']) == false
10+
*/
11+
12+
function arr2bin(arr){
13+
if(arr.filter(v=>typeof v != 'number').length>0) return false
14+
return arr.reduce((a,b)=>a+b,0).toString(2)
15+
}

0 commit comments

Comments
 (0)