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 trialJuan Juarez Baldizon
Courses Plus Student 3,352 PointsNot 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.
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++
}
1 Answer
Jennifer Nordell
Treehouse TeacherHi 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:
Juan Juarez Baldizon
Courses Plus Student 3,352 PointsThanks 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
Treehouse TeacherJuan 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++
}
Juan Juarez Baldizon
Courses Plus Student 3,352 Pointsit still gives me the error, any idea how I can fix this?
Jennifer Nordell
Treehouse TeacherJuan Juarez Baldizon No, I have no idea. This is the answer confirmed by many. My next suggestion is that you contact Treehouse support.
Juan Juarez Baldizon
Courses Plus Student 3,352 PointsJuan Juarez Baldizon
Courses Plus Student 3,352 Pointsyeah, I'll do that, thanks for the quick reply