From 7ad46604a37551d9012a76ccec9cdd7d1b23ee73 Mon Sep 17 00:00:00 2001 From: Blaine Date: Tue, 15 Sep 2020 14:35:11 -0700 Subject: [PATCH] final b-to-d --- lib/binary_to_decimal.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..a47f23b 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,23 @@ # 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 + +# Take binary numbers and calc them from right to left based on the number of index options +# What am I trying to get this code to execute? + + + +binary_arr = Array.new(8) { rand(2) } + +p binary_arr + +def binary_to_decimal(binary_arr) + total = 0 + binary_arr.each_with_index do | onezee, index | + rev_index = binary_arr.length - 1 - index + total += onezee * 2 ** rev_index + end + return total end + +p binary_to_decimal(binary_arr)