From 1a2bd127b32880042b471c2f5dc3b017050cdfde Mon Sep 17 00:00:00 2001 From: Alexandria Brown Date: Sun, 25 Feb 2018 13:37:44 -0800 Subject: [PATCH] binary_to_decimal passs all test --- lib/binary_to_decimal.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..bf46d2d 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,21 @@ # 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. + +require 'awesome_print' +require 'pry' + def binary_to_decimal(binary_array) - raise NotImplementedError + indices = [] + (0..binary_array.length - 1).each do |i| + reverse_index = (binary_array.length - 1) - i + indices << reverse_index + end + + total = 0 + (0..binary_array.length - 1).each do |i| + value = binary_array[i] * (2 ** indices[i]) + total += value + end + return total end