Making my function calculate average of array Swift
I want my function to calculate the average of my Double type array. The array is called "votes". For now, I have 10 numbers. When I call the average function to get the average of the array votes, it doesn't work. Here's my code: var votes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] func average(nums: Double...) -> Double { var total = 0.0 for vote in votes { total += vote } let votesTotal = Double(votes.count) var average = total/votesTotal return average } average[votes] How do I call the average here to get the average? You should use the reduce() method to sum your array as follow: Xcode 10 • Swift