Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Ada-C14/binary-and-decimal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: kashenafi/binary-and-decimal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Sep 12, 2020

  1. Copy the full SHA
    9a77aba View commit details
Showing with 15 additions and 1 deletion.
  1. +15 −1 lib/binary_to_decimal.rb
16 changes: 15 additions & 1 deletion lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
@@ -5,5 +5,19 @@
# 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
parts = []
exponent = 0

binary_array.reverse.each_with_index do |val, ind|
parts << (val * (2** exponent))
exponent += 1
end

decimal_number = parts.inject(0){|sum,x| sum + x }

return decimal_number
end

binary_array = Array.new(8) { rand(0..1) }

binary_to_decimal(binary_array)