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 trialWilliam Hartvedt Skogstad
5,857 PointsWorking with Loops, Challenge Task 2 of 2
I can't figure out what they want me to do in this code challenge, how should I approach this?
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.
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.count {
sum = sum + newValue
counter++
}
15 Answers
ianhan3
4,263 Points- "Using the value of counter as an index value, retrieve each value from the array"- so you want to set up the numbers array in the loop to evaluate at each index (i.e. numbers[0], numbers[1], etc.) as shown below:
numbers[counter]
- and add it to the value of sum. - easy enough, add to the previous sum and assign that to the new sum.
sum = sum + numbers[counter]
Each time the loop evaluates a index value of the numbers array, it will grab the value (numbers[0] = 2), add the previous sum (no previous sum in number[0] so 2+0) and add it to the new sum (2 + 0 = 2 <- the new sum). Then that repeats for each of the index values in the numbers array.
Full code should be:
while counter < numbers.count {
sum = sum + numbers[counter]
counter++
}
Ashvin Mbuguni
6,360 PointsI feel that this swift course isn't very good, because I've encountered content that hasn't been taught and the format of questioning in numerous challenges are not very clear. Treehouse need to fix this ASAP!
Josue Gisber
Courses Plus Student 9,332 Pointsyour code looks good, the only thing you have to change is the newValue variable for number[counter]
Shandas Duchintav
8,222 Pointsurgh I still can't get this one right. The code above doesn't work.
William Hartvedt Skogstad
5,857 PointsShow me your code and I might be able to help.
William Humphrey
5,543 PointsFor Swift 3 C++ has been removed and replaced with +=; therefore let numbers = [2,8,1,16,4,3,9] var sum = 0 var counter = 0 while counter < numbers.count { sum = sum + numbers[counter] counter += 1 }
Adam Mazurick
Courses Plus Student 7,653 PointsI couldn't figure this out and in the solution provided above, I find this line a little confusing. Can you please clarify this? Specifically what we're doing with numbers[counter]?
sum = sum + numbers[counter]
William Hartvedt Skogstad
5,857 PointsWhat you're doing is setting a new value for "sum" to the total of "sum" + the value of each index in the "numbers" array. This will cause the var "sum" to display all the numbers in the "numbers" array as one total.
Example:
let numbers = [1, 2]
var sum = 0
var counter = 0
while counter < numbers.count {
sum = sum + numbers[counter]
counter++
}
// The var "sum" should now display the value 3
This specific "while loop" performs the addition of the numbers in the "numbers" array while the var "counter" is less than the number of ints in the "numbers" array (hence the "numbers.count).
I hope this helped :)
Josue Gisber
Courses Plus Student 9,332 Pointssum = sum + numbers[counter]
, this is telling the compiler: **Get whatever number is in the array numbers and add that number to the variable sum. ** So every time the while loop goes, it grab a number from the array and add that number to whatever number exist in the sum variable.
i hope this make sense.
Shandas Duchintav
8,222 PointsI got it. Thanks for the reply!
Matthew Corry
Courses Plus Student 4,389 Pointswhile counter < numbers.count { sum = sum + numbers[counter] counter += 1 }
Paschal Ihenacho
914 Pointswhile counter < numbers.count { sum = sum + numbers[counter] counter += 1 }
Paolo Di Donato
4,945 PointsMy current code is below and I am finding an error still. I have even copied and pasted what someone posted as the correct answer... Help is appreciated!
let numbers = [2,8,1,16,4,3,9] var sum = 0 var counter = 0
// Enter your code below
while counter < numbers.count { sum = sum + counter[numbers] counter += 1 }
Jos Feseha
4,011 Pointswhile counter < numbers.count { sum = sum + numbers[counter] counter += 1
}
Jos Feseha
4,011 PointsThis is the write answer
Paolo Di Donato
4,945 PointsRight, thank you so much.. devils in the details
Eddie Flickinger
3,513 PointsThe answer passes, but will never run through on the play ground. I keep getting type 'Int' has no subscript members
malcolm henry
1,089 PointsI Feel somethings havent been taught prior to code challenge -- i haven't seen ++ not a single time
- this is frustrating af
Nasra Iid
Front End Web Development Techdegree Student 9,876 PointsDid anyone find the answer to this,,,,?
William Hartvedt Skogstad
5,857 PointsWilliam Hartvedt Skogstad
5,857 PointsThanks again for the detailed description! It really helps me understand everything better :)