From 6b2aec80d4ab4423f3492dc53986bd2080da25b1 Mon Sep 17 00:00:00 2001 From: ranuseh Date: Mon, 4 Mar 2019 20:17:24 -0800 Subject: [PATCH] Assignment Completed --- lib/binary_to_decimal.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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)