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

I don't know how to complete this challenge

it says that after creating the while loop, using the value of counter as an index value you need to retrieve values from the array and add it to the value of sum.i tried many times and I couldn't figure out how to do this.

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
   for newValue in numbers.count {
    sum += newValue
   }

}

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Musthakeem,

You don't want to have a second loop inside your first loop. Remember that your while loop is going to run as many times as there are items in your array. Each time your while loop runs, you have a new value for counter that can be used as an index into your array to get the value to add to sum.

Also remember that you can access a value in an array using subscript notation:

myArray = ['a', 'b', 'c']
myValue = myArray[1]  // 'b'

Hope that helps

Alex