iOS Swift Collections and Control Flow Control Flow With Loops Working With Loops

how to sum/retrieve value of arrays?

this isnt making sense or i skipped something -- help?!

loops.swift
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
var newValue = 0

// Enter your code below

while counter < 6 {
  print("numbers")
  counter += 1
  print(numbers[sum])
}

1 Answer

let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
var newValue = 0

// Enter your code below

while counter < numbers.count {
  sum += numbers[counter]
  counter += 1
}

Changing the loop to the above code will step through your array, add up the numbers, and increment your counter.