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

Jericoe States
Jericoe States
3,262 Points

cannot figure out this code.

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

while sum < 6 { print(sum) sum += 1 }

Using the value of counter as an index value, retrieve each value from the array and add it to the value of sum. For example: sum = sum + newValue. Or you could use the compound addition operator sum += newValue where newValue is the value retrieved from the array.

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

while sum < 6 {
    print(sum)
    sum += 1
}

6 Answers

// counter += 1 will increase the counter from 0 to 7 on each iteration of the loop. So when it is 7, the loop will end.

//you can contantenate strings with the + like "Hello" + " World" = "Hello World". So += operator also works. However this challenge is only working with integers so += works.

// the counter is just an integer (not an array). So with each iteration of the loop of

Sum += number[counter] is actually: Sum += number [0] Loop Sum += number[1] Etc....

Hint: your code is not getting the last integer in the array. It stops before that.

Jericoe States
Jericoe States
3,262 Points

I don't understand.

let number = [1,2,3,4,5,6,7,8,9] --- this the array collection

var sum = 0 var counter = 0 --- these are integers that were giving in the question. I'm not to sure whether they are supposed to remain as zero's or not.

while sum < 6 { print (sum) } --- I can't really explain whats going on here. I don't understand how to use the value of counter as an index value, retrieve each value from the array and add it to the value of sum.?

No worries. It can be a little tricky at first. I'm trying to not give you actual code until you understand it. Hope it helps.

A: numbers becomes an array of integers

let numbers = [2,8,1,16,4,3,9]

A: you are correct. One is a counter and one keeps track of the total sum

var sum = 0 var counter = 0

A: your code below is using the wrong variable as a counter. Remember, the 'while' loop will repeat as long as it is less than your counter. Also make sure your " < 6 " is the right number needed.

while sum < 6 {

A: the directions doesn't say you need to print anything so you could remove this

print(sum)

A: next you want to get the integer in the array that you want to add to the sum

sum += yourArrayVariable[counter]

A: as before, your using the wrong variable as a counter. Change variable to your counter variable, now increment your counter.

sum += 1

}

//there is another way to finish this excercise using "for" loops but you can complete it doing with a while loop as well. I'll show you the for loop after.

Let me know if this helps or is more confusing.

Jericoe States
Jericoe States
3,262 Points

// after looking at this for 3 days, this is were I am.

(1)let numbers = [2,8,1,16,4,3,9] // line 1. is correct.

(2)var sum =43 // sum = the SUM of numbers in the array, I want this to equal 43,, but my gut is telling me thats wrong. Though it could be right.

(3)var counter = 7 // count = how many integers are in the array. if an arrays count always includes zero,, that mines should look something like this (0,1,2,3,4,5,6)

(4)while counter < 7 { // here is where things start to get foggy.. I feel like this is saying if counter is less than 7 continue the while loop

(1.1) counter += 1 // and the += 1 is what stop the system from running endlessly causing it to crash.

(1.2) sum += counter // This last line has my mind blow,, I feel like I should use (sum += number.count) but now I'm just guessing

}

Ive been stuck to long and now I'm starting to over think the question. :S PS: you trying to help me solve it so I can have a better understanding is a great method! (3thumbs up)

Jericoe States
Jericoe States
3,262 Points

My codes without the notes/comments

var numbers = [2,8,1,16,4,3,9]

var sum = 43

var counter = 7

while counter < 7 {

counter += 1

sum += numbers.count

}

Great job so far you are almost there. I'm impressed that you've stuck with it that long.

Notes/Hints:

Sum and counter will both start at 0. (By the end they will end up as 43 and 7)

You don't have to but it's common practice to put the counter += 1 at the end. So move the sum += ... up one line.

You are very close and have the right concept but wrong syntax for the sum line. To access the index in the array you would use array[3].

Ex: sum += array[x]

Let me know if you need more help but I think you got it. I'm glad not giving the exact answer is what you prefer because some don't.

Jericoe States
Jericoe States
3,262 Points

var numbers = [2,8,1,16,4,3,9]

var sum = 0

var counter = 0

while counter < 0 {

sum += numbers[43]

counter += 1 }

I can't get it? 5 days I give up, I need to see it

// I hate that you don't tell me, but in this case of learning code it exactly what I would recommend. Mind you I am ready for the answer.

var numbers = [2,8,1,16,4,3,9]

var sum = 0

var counter = 0

while counter < 7 {

sum += numbers[counter]

counter += 1
}
Jericoe States
Jericoe States
3,262 Points

Sum += number[counter]

// counter += 1, gives counter a value of 1 so the program does crash or run endlessly.

// but how can you += anything other an integer, I thought you could only (+=) integers?

// why is the constant |counter| in an array --> [counter], the only array I have is the one I created with the variable constant numbers?