From 372f17a49f3046f2d5ec9dc22c886e23ed48c08d Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Mon, 26 Feb 2018 15:51:04 -0800 Subject: [PATCH 1/2] Finished code --- lib/binary_to_decimal.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..eb121af 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -1,9 +1,33 @@ +require 'minitest/autorun' +require 'minitest/reporters' # A method named `binary_to_decimal` that receives as input an array of size 8. # The array is randomly filled with 0’s and 1’s. # The most significant bit is at index 0. # 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. +#Creates randomly filled array with 0's and 1's, 8 times. + +#Create method named binary_to_decimal to return decimal value. input is binary array. + +binary = [] +8.times do + binary << rand(0..1) +end + def binary_to_decimal(binary_array) - raise NotImplementedError + subtotals = [] + p binary_array + + binary_array.length.times do |i| + subtotals << binary_array[i] * ((2) ** ((binary_array.length - 1) - i)) + end + + decimal_number = 0 + subtotals.each { |subtotal| decimal_number += subtotal } + # p decimal_number + # return decimal_number end + +decimal_number = binary_to_decimal(binary) +return decimal_number From 66769cd0e20710ead906c3a74b4751af83c20775 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Mon, 26 Feb 2018 16:03:46 -0800 Subject: [PATCH 2/2] Final Changed --- lib/binary_to_decimal.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index eb121af..0d40df1 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -10,14 +10,13 @@ #Create method named binary_to_decimal to return decimal value. input is binary array. -binary = [] -8.times do - binary << rand(0..1) -end +# binary = [] +# 8.times do +# binary << rand(0..1) +# end def binary_to_decimal(binary_array) subtotals = [] - p binary_array binary_array.length.times do |i| subtotals << binary_array[i] * ((2) ** ((binary_array.length - 1) - i)) @@ -25,9 +24,5 @@ def binary_to_decimal(binary_array) decimal_number = 0 subtotals.each { |subtotal| decimal_number += subtotal } - # p decimal_number - # return decimal_number + return decimal_number end - -decimal_number = binary_to_decimal(binary) -return decimal_number