Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@
# 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
# def binary_to_decimal(binary_array)
# raise NotImplementedError
# end

def binary_to_decimal(random_array)

results = []
exponent = 7

random_array.each do |num|
# puts "exponent before conversion #{exponent}"

conversion = num * 2**exponent
# puts "Current result #{conversion}"

results << conversion

exponent -= 1
# puts "exponent after conversion #{exponent}"
end

return results.sum

end


# random_array = Array.new(8) { rand(0..1) }
random_array = [1, 0, 0, 1, 1, 0, 0, 1]
puts "This is the random array #{random_array}"

puts "These are your binary numbers in decimal form #{binary_to_decimal(random_array)}"