diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..14937c6 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,12 @@ # The least significant bit is at index 7. # Calculate and return the decimal value for this binary number using # the algorithm you devised in class. -def binary_to_decimal(binary_array) - raise NotImplementedError + +array = Array.new(8) { rand(0..1) } + +def binary_to_decimal(array) + reversed_array = array.reverse + return reversed_array.map.with_index {|value, index| (2**index) * value}.sum end + +puts binary_to_decimal(array)