-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug02.rb
More file actions
47 lines (39 loc) · 894 Bytes
/
Copy pathdebug02.rb
File metadata and controls
47 lines (39 loc) · 894 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def average(numbers)
if numbers == nil
return nil
end
numbers.map! { |i| i.to_f }
sum = 0
numbers.each do |num|
sum += num
end
if numbers.count == 0
return nil
end
sum / numbers.count
end
## TEST HELPER METHOD
def test_average(array=nil)
if array == nil
result = nil
else
print "avg of #{array.inspect}:"
result = average(array)
end
p result
end
## TEST CODE
test_average([4,5,6]) # => 5
test_average([15,5,10]) # => 10
# Should treat string like number
test_average([15,"5",10]) # => 10
# Should show decimal value
test_average([10, 5]) # => 7.5 instead of just 7
# Watch out! Even tests can have bugs!
test_average([9, 5, 7])
#Empty set should return nil, not throw an error
test_average([]) # => nil
# Non-existent set should return nil
test_average() # => nil
# BONUS: Should ignore nils in the set
test_average([9,6,nil,3]) # => 6