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

Mike Bailey
Mike Bailey
823 Points

Struggling with while loops...

Here's the question challenge:

In this task, we have an array of numbers and we want to compute the sum of its values.

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)


Can someone just help me start off and point me in the right direction?

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

// Enter your code below

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Mike! Let's try with some pseudocode and see if you can get the syntax down. Although, I'd be interested to know what you've tried so far so as to optimise the learning experience for you.

while the counter is less than the number of elements in the numbers array
    add the number in the array that's at the same index as counter to the total sum
    increment the counter
end while

Hope this helps, but let me know if you're still stuck! :sparkles:

Mike Bailey
Mike Bailey
823 Points

Thanks Jennifer - I got this far, not sure if it's the right direction or not, but it's blowing out in Playgrounds

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

// Enter your code below

while counter < numbers.count {
    sum.append = numbers

}
Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're not quite there yet. Remember, we use append when we want to append to an array. But sum isn't an array. It's just a plain old integer. Also, we don't want to append anything to our numbers array, it should stay as it is. What we're attempting to do here is go through that entire array and add up all the numbers in it.

However, the while part that you have is spot on! It's the inside code that's not doing what you think it should. I'm going to post my solution with comments to explain:

while counter < numbers.count { //while the counter is less than the number of items in the numbers array
    sum += numbers[counter] //get the number at the index equal to counter.  The first iteration will get number[0] which is 2 etc
    counter += 1 // increment the counter.  The next iteration the counter will be 1
}

Hope this helps! :sparkles:

Mike Bailey
Mike Bailey
823 Points

Thanks Jennifer. What you've posted makes sense. It's easy working along with the videos but the practicals do stump me. Thanks.