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 2.0 Collections and Control Flow Control Flow With Loops Working with Loops

Swift Challenges

I need help with these 2 challenges:

Challenge Task 1 of 2

This one might be a bit tricky so let's take it in steps. We have an array of numbers, nothing tricky, you've seen this before. What we want is to compute the sum of the numbers in the array. To do this we're going to use a while loop.

We have a variable ,sum, that will store the value of the sum of numbers from the array.

We also have a variable ,counter, which we will use to track the number of iterations of the while loop.

Step 1: Create a while loop. The while loop should continue as long as the value of counter is less than the number of items in the array. (Hint: You can get that number by using the count property)

my code no work

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

// Enter your code below

while counter < numbers.count {
print(numbers[counter])
counter++
}
sum = 

Challenge Task 2 of 2

Now that we have the while loop set up, it's time to compute the sum! 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.

no work BUMMMM

while.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++}


while counter < numbers.count {
    print(numbers[counter])
    counter++    
}
sum =  

9 Answers

I've put you a solution in your other post. Again, you're very nearly there - just slightly out on the while loop construction.

You've correctly identifed that you want to loop while counter is less than numbers.count. That's a great start. You then want to make sure you increment counter inside the loop else it'll never end!

Before that, take the array element and add it to sum. The element is numbers[counter]. That all comes out like:

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

Steve.

Austin Lockard
Austin Lockard
2,936 Points

This may be a dumb question, but I need help understanding this statement: print(numbers[counter]). I get that numbers is a constant, so what does adding [counter] to the end of the statement actually do? Thank you

Hi Austin Lockard ,

The constant, numbers is an array. It holds multiple values in a prescribed order. That order can be accessed by using a number to ask for the value stored at each particular place, i.e. 1st element, 2nd element etc. It has 7 elements in this example. Its definition is:

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

Each element of an array can be accessed by accessing its position within the array. We use the square brackets to do that, such that:

numbers[0] holds the value 2.

numbers[1] holds the value 8.

numbers[2] holds the value 1.

... and so on.

So, if we are looping, i.e. running the same portion of code repeatedly, and also incrementing the variable counter at each loop, using counter to access numbers allows us to iterate through every value that numbers holds. The counter is set to zero initially:

counter is 0 so numbers[counter] holds the value 2.

counter is 1 so numbers[counter] holds the value 8.

counter is 2 so numbers[counter] holds the value 1.

What we then do with that value is irrelevant to the point. You can use print to display it on the screen or add it to sum using += to add up the contents of the array. But that's what the square brackets, combined with counter, do.

I hope that helps.

Steve.

Austin Lockard
Austin Lockard
2,936 Points

Thank you. That helps a ton. I just wasn't understanding where counter fit in the scheme of things. But I understand the logic behind it now. Thank you again.

No problem, Austin

Glad to help.

Steve.

Robert Hand
Robert Hand
Courses Plus Student 2,657 Points

I'm having trouble with Challenge Task 2 of 2...here's what I had for Task 1 of 2:

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

// Enter your code below
while counter < numbers.count {
    print(numbers[counter])
    counter += 1
}

Task 2 - this is what I've tried and doesn't work:

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

Steve, I've tried the way as you've written it out and it's not working for me....I'm also not familiar with counter++

Daniel Walker
Daniel Walker
2,031 Points

this way worked for me

Part 1/2

while counter < numbers.count
     { counter += 1}

Part 2/2

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

}
Blake Bassett
Blake Bassett
3,448 Points
while counter < numbers.count {
    print(numbers[counter])
    counter += 7
}

Hi Blake,

Why increment by 7?

Steve.

Hi Robert Hand ,

First of all, you're pretty much there with your code. But you don't want to print anything. So, change your line:

print(sum += numbers[counter])

to:

sum += numbers[counter]

That should do it.

The reason you're unfamiliar with counter++ is because this was deprecated in Swift 3.0. The course that this thread was about was an earlier version of Swift where the unary increment operator, ++, was allowed (as it is in most other languages). It was a convenient way of adding one to a number and came with its brother, --, which did the opposite.

Let me know how you get on.

Steve.

Robert Hand
PLUS
Robert Hand
Courses Plus Student 2,657 Points

That worked. Thanks so much for the quick response Steve!

No problem! :+1:

michael duaybes
michael duaybes
431 Points

Wow thank you, I see how it is done now since counter is +1 in the array numbers. Nice!!!

Nathalie Dory
Nathalie Dory
2,912 Points

so while the variable -counter- (first at zero) is less that the total number of integers (there are 7) in the array -numbers, we are going to add the integer at position zero to sum and apply it back to the variable sum. The first integer at position zero is 2, so sum will now hold the value of two. We then add 1 to the counter and run the loop again, so while the counter (now valued at 1) is less than the total number of integers in the numbers array (still 7) we are going to add the sum, now valued at 2, to the integer at position one in the array, which is the second one, 8, which makes 10, which is applied back to sum, so the value of sum is now 10. Then we add one to the counter, making the counter 2 which is still less than 7 so we repeat until each integer is added to sum.

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

counter+=1

}

while counter < numbers.count {
    print(numbers[counter])
    counter += 1
}

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