diff --git a/Rakefile b/Rakefile index 0c2d13f..fb007c6 100644 --- a/Rakefile +++ b/Rakefile @@ -6,4 +6,4 @@ Rake::TestTask.new do |t| t.test_files = FileList['test/*_test.rb'] end -task default: :test +task default: :test \ No newline at end of file diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..8a0fa1e 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,20 @@ # 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 + +binary = Array.new(8) {rand(0..1)} + +def binary_to_decimal(binary) + puts binary.join + "\n" + a = binary.length + decimal = 0 + + for i in 0...a do + reverse_index = 7 - i + num = binary[reverse_index].to_i * 2**i + decimal +=num + end + return decimal end + +binary_to_decimal(binary)