Skip to content
Open
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions lib/array_equals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,51 @@
# and the same integer values in the same exact order
def array_equals(array1, array2)
raise NotImplementedError
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Make sure to remove the raise here, otherwise it will throw an exception.

if array1 == nil || array2 == nil
Copy link
Copy Markdown

@ChubbyCub ChubbyCub Mar 1, 2019

Choose a reason for hiding this comment

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

When I think about how to write effective if-statements, I normally think of the flow like a triangle, where the stricter conditions should be going first, and the less strict conditions follow. The reason is if what you want to check does not meet the stricter conditions, you can quickly return the result without wasting time moving through the rest of the less strict conditions.

Applying the principle above to this particular problem, I will put the && before the | |.

return true
elsif array1 == [] && array2 == []
return true
elsif array1 == nil || array2 == nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The statements on line 9 & 10 seem contradictory to the statement on line 5 & 6. Could you please look into it?

return false
elsif array1.length != array2.length
return false
index = 0
while index < array_length
if array1[current_index] != array2[current_index]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think you mean index (instead of current_index) here?

return false
end
index += 1
return true
else
return false
end
end
end
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code is clear, concise and easy to read. Good work!


# NOTES WHILE WORKING THROUGH

# if
# array1.length =! array2.length
# returns false
# break

# index = 0
# while index < array.length
# element = array[index]
# #do operation
# index += 1
# end

# array_equals

# false?
# arrays are not exactly the same
# element values different
# elements not in the same order

# true?
# arrays have some # of elements
# element values are the same
# order is the same