diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..ed21364 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,43 @@ # 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 + + n = 0 + expected_decimal = 0 + + expected_decimal += binary_array[7].to_i * (2**(n)) + puts expected_decimal + n += 1 + + expected_decimal += binary_array[6].to_i * (2**(n)) + puts expected_decimal + n += 1 + + expected_decimal += binary_array[5].to_i * (2**(n)) + puts expected_decimal + n += 1 + + expected_decimal += binary_array[4].to_i * (2**(n)) + puts expected_decimal + n += 1 + + expected_decimal += binary_array[3].to_i * (2**(n)) + puts expected_decimal + n += 1 + + expected_decimal += binary_array[2].to_i * (2**(n)) + puts expected_decimal + n += 1 + + expected_decimal += binary_array[1].to_i * (2**(n)) + puts expected_decimal + n += 1 + + expected_decimal += binary_array[0].to_i * (2**(n)) + + return expected_decimal end + +# raise NotImplementedError