Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Rebecca Spiller
Rebecca Spiller
461 Points

Code Challenge 2 - While Loops

Okay, I don't know whether I've just crammed too much in too short a time or if I'm truly missing something obvious here. My first task was to create this while loop:

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


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

The next part of the task is as follows: 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.

I keep drawing a blank, no matter how many times I read the question. I know it's probably something very basic as well. Please can someone break it down for me?

Thank you!

Looks fine - what error is it giving you? (I'm just finding the challenge now ... )

Rebecca Spiller
Rebecca Spiller
461 Points

Sorry Steve, it's the second part of the challenge which is where I'm getting stuck. Computing the sum itself.

3 Answers

OK Rebecca,

That's fine. You clearly understand it as you've got the code correct!

So, we're iterating of an array of numbers. That's called numbers. To make sure we're not stuck in a continuous loop, we're using a counter variable which increments at each iteration. The loop stops when counter is no longer less than the length of the array.

Inside the loop, aside from the incrementation, we're adding up each of the elements within the array. We access arrays by using square brackets and an index number. So, let's take:

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

... this array. You access the first element (we start counting at zero for this) by using numbers[0]. That gives us access to the first element, the integer '2'. In this, we're wanting to access each element in turn. So we're utilising the counter variable as the index to access the array with. So, we set counter to zero at the outset, and in the first loop within the while loop, we use numbers[counter] to perform the same task as numbers[0]. In each successive loop, the value held by counter is increased by one, giving us access to numbers[1] then numbers[2] etc, until we run out of elements and the loop stops.

Now we've accessed each element in turn, we add up each one into the sum variable. Again, this is set to zero at the outset. We then add into it numbers[0]. You've used += which adds the value to the right into the existing value on the left of it. You could use the longhand version:

sum = sum + numbers[counter]

// same as 

sum += numbers[counter]

By the time the loop has ended, you have added every element of the array into sum.

Does that clear things up a bit?

Steve.

Hi Rebecca,

Your code passed the challenge here - I suggest copying it and refreshing the challenge, then pasting the code back in.

Steve.

Rebecca Spiller
Rebecca Spiller
461 Points

Hi Steve,

It's the second task of that challenge - computing the sum. I've re-read the question several times and it's just not making sense. Thanks!

Rebecca Spiller
Rebecca Spiller
461 Points

Thank you! Explaining like that clears up in my mind which part is the computing the sum. Sorry if I didn't make myself very clear, I think my brain is a little bit frazzled from cramming so much into it.

No problem! :+1: As long as it is clear now ... :smile: