From ec6c118adc36ccaa44f84bdcbef209aacb096b76 Mon Sep 17 00:00:00 2001 From: Madeline <40412372+made-line@users.noreply.github.com> Date: Tue, 15 Sep 2020 18:50:02 -0700 Subject: [PATCH] Update binary_to_decimal.rb --- lib/binary_to_decimal.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..350cc1f 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,19 @@ # 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. +binary_array = [] + +8.times {binary_array << rand(0..1)} + def binary_to_decimal(binary_array) - raise NotImplementedError + sum = 0 + binary_array.each_with_index do |binary, index| + exponent = binary_array.length - 1 - index + value = binary * 2 ** exponent + sum += value + end + # raise NotImplementedError + return sum end + +print binary_to_decimal(binary_array)