Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Nicholas Moore
Nicholas Moore
1,117 Points

I'm stuck on this challenge

I know the concept of the while loop, but I don't know how to retrieve the values in the array

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

// Enter your code below
while counter < numbers.count {
counter += 1
var newValue = numbers[]
sum += number
}

1 Answer

var newValue = numbers[counter]

Index - that's how you access an item in array. For example, if our array was [1,2,3] and counter had a value 0, numbers[counter] would actually mean numbers[0] and that means "I want an item which is in this array on position 0" which in this case returns 1 :)