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

Not clear on question for while loop in part 2 of challenge question, need clarification

I can' seem to isolate what the second part of the question is asking, I'm aware of the concept of the index but can't seem to get the newValue variable to come into play, please clarify, I get stuck where the message says I need calculation of sum? Do I need to use interpolation to calculate for the newValue? Please help.

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

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

yeah, I'll do that, thanks for the quick reply

1 Answer

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

Hi there! Let's see if you can get it after I explain the instructions. What it wants is for you to make a for loop that will run down the length of the array it provides. We're going to keep track of where we are in the array with our counter variable. At the end, we should have the sum of all the numbers in that array. So if we start with counter equal to zero as it does here then we take sum and add the number at the array index of 0. That will be 2. So now our sum variable should be equal to 2. Then we increment counter. The next time through the loop the counter is equal to 1 and our array has 8 at the index of 1. Now we'll add 8 to sum which will give us a new running total of 10. And we continue this way until the end of the array. Good luck! But if you just desperately need the answer I previously posted a solution to this along with an explanation here:

https://teamtreehouse.com/community/cant-get-through-this-spent-an-hour-on-it-really-feels-instructions-are-unclear-help-please

Thanks for the reply, I checked your answer on that older post and answer still won't work. It says Make sure your condition allows for iteration over all elements in the numbers array and check for your calculation of sum. This is strange because I thought the 2nd loop does go through the whole array? If you try the challenge question does your answer not return this error?

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

Juan Juarez Baldizon That's odd. Because if I take this code I'm going to post below and copy and paste it into the challenge it passes both steps.

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

// Enter your code below
while counter < numbers.count {
  sum += numbers[counter]
  counter++
}

it still gives me the error, any idea how I can fix this?

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

Juan Juarez Baldizon No, I have no idea. This is the answer confirmed by many. My next suggestion is that you contact Treehouse support.