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!
Adds a new element at the end of the array.
To append or concatenate two arrays in Swift, use plus + operator with the two arrays as operands.
If you are using List as an array, you can use its append(), insert(), and extend() functions. You can read more about it at Python add to List. If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array.
No loop needed. Use reduce
, like this:
let sum = array.reduce(0,+)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With