Reputation: 255
I have an array of numbers and I want to iterate through all of the elements in that array and add together all of the integers. Here is the function I have so far:
func addTogether(array:Array<Int>, divide:Int) -> Int
{
var a = 0
while a < array.count
{
}
return 0
}
I know that I'm probably going to have to do this inside of the while loop. Can anyone give me some guidance as to where to go from here? Thanks!
Upvotes: 7
Views: 6625
Reputation: 534925
No loop needed. Use reduce
, like this:
let sum = array.reduce(0,+)
Upvotes: 23