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

Not sure my brain is cut out for this anymore, and I just started a few days ago!

I'm trying to figure out how I can make the numbers array indexes add themselves together in this loop. there is some kind of logic that I guess I forgot.

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
    sum += numbers[counter]
}
mrtroy
mrtroy
8,069 Points

Let me give you a hint because you are very close to the answer to this challenge.

The first index of an array is zero and the first thing you do in the while loop is increment that.

On a different note if you are wanting to get a sum of the indexs not the values you will need to setup another variable, but I don't think that is what the challenge is asking for. Look close. You are almost there.

1 Answer

You are very close.

  1. Increment the counter after you add to the value of sum. Remember that the first object in an array starts at 0. So if you increment first you will never add the first number in the array. So if you want to access the first number in the array which is 2 you would access it like this: array[0]

  2. When adding the Int to sum you need too assign it to sum as well.

    sum = sum + numbers[counter]
    

Also every time you get passed something you didn't understand just think of it like doing a push up for your brain. I remember when I first watched the video on completion handlers I was like wtf I will never get this. Now I'm like how did I not get this lol.

Thank you a lot sir. You give me a lot of motivation to persevere.