forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIndex.rb
More file actions
27 lines (20 loc) · 689 Bytes
/
ArrayIndex.rb
File metadata and controls
27 lines (20 loc) · 689 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
=begin
Complete the solution. It should try to retrieve the value of the array at the
index provided. If the index is out of the array's max bounds then it should
return the default value instead.
Example:
data = ['a', 'b', 'c']
solution(data, 1, 'd') # should == 'b'
solution(data, 5, 'd') # should == 'd'
# negative values work as long as they aren't out of the length bounds
solution(data, -1, 'd') # should == 'c'
solution(data, -5, 'd') # should == 'd'
=end
# My Solution
def solution(items, index, default_value)
index.abs > items.length ? default_value : items[index]
end
# Another Solution
def solution(items, index, default_value)
items.fetch(index, default_value)
end