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
7 changes: 6 additions & 1 deletion lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
# 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
i = 0
decimal = 0
binary_array.length.times do |i|
decimal += binary_array[i] * 2 ** (7 - i)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I think your code looks great! It's very concise! One comment that I have is on line 11, in the case where you would want to scale this program to intake binary number greater than 8 numbers, you might want to modify exponent of the 2 (the 7-i part). Perhaps setting the binary_array.length = n and the exponent would be (n-i). For example if the binary_array.length = 9 then the exponent would be (9-i) Let me know if this does not make sense :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great feedback! To address this feedback, how about leveraging the length of the array? Instead of 7, use binary_array.length - 1.

return decimal
end