-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_arrays.rb
48 lines (44 loc) · 978 Bytes
/
2_arrays.rb
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
### SETUP
require 'rspec'
### LOGIC (fix me)
# Returns the average of all the numbers in the array
def average(numbers)
# Why doesn't
# if numbers == [] || nil
# work? I had to split them...?
if numbers == []
nil
elsif numbers == nil
nil
else
numbers.map! {|n| n.to_f}
sum = 0
numbers.each do |n|
sum += n
end
sum / numbers.size
end
end
### TEST CODE (don't touch me)
describe '#average' do
it 'returns nil for empty array' do
result = average([])
expect(result).to be_nil
end
it 'returns nil when nil is passed in' do
result = average(nil)
expect(result).to be_nil
end
it 'returns 4 for 3,4,5' do
result = average([3, 4, 5])
expect(result).to eq(4)
end
it 'can handle numbers represented as strings' do
result = average([10, '20', 30])
expect(result).to eq(20)
end
it 'can handle floats' do
result = average([1.0, 1.5, 2.0])
expect(result).to eq(1.5)
end
end