From 364de08280db9031c142c0193771dd74f915d5f7 Mon Sep 17 00:00:00 2001 From: laneia Date: Thu, 28 Feb 2019 08:32:16 -0800 Subject: [PATCH] submission --- lib/binary_to_decimal.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..8d66706 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,17 @@ # 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 + i = 0 + decimal_values = [128, 64, 32, 16, 8, 4, 2, 1] + decimal_array = [] + + 8.times do + if binary_array[i] == 1 + decimal_array << decimal_values[i] + end + i += 1 + end + return decimal_array.sum end