diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..5765d34 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -5,5 +5,13 @@ # 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 + sum = 0 + exp = binary_array.length - 1 + binary_array.each do |x| + if x == 1 + sum += 2 ** exp + end + exp -= 1 + end + return sum end diff --git a/specs/binary_to_decimal_spec.rb b/specs/binary_to_decimal_spec.rb index ba17713..0cd87bf 100644 --- a/specs/binary_to_decimal_spec.rb +++ b/specs/binary_to_decimal_spec.rb @@ -1,6 +1,7 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require_relative '../lib/binary_to_decimal' + +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/binary_to_decimal" describe "binary to decimal" do it "From 10011001 to 153" do