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 trialRyan Maneo
4,342 PointsIt works perfectly in the console... whats wrong with my code???
I don't get it...
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.count {
let newValue = numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4] + numbers[5] + numbers[6]
sum += newValue
counter++ // will be replaced with counter += 1 in Swift 3 as ++ is deprecated
}
2 Answers
David Papandrew
8,386 PointsRyan,
No, they don't do the same thing. If you check the value of the sum variable for your solution (in playgrounds), the sum is 301. This is because you are adding the sum of all the array values (43) each time the while-loop runs. The while-loop runs 7 times (7*43 is 301).
In the correct solution, the while loop adds a single array item to the sum on each pass.
Also, consider this: If the array consisted of 200 items would your initial solution still work without any tweaks? You'd actually have to hardcode all the values with that approach. One goal of iteration is to take out the drudge work and let the computer do the heavy lifting. With the correct solution, you can enter any size array for "numbers" and it will work.
Hope that helps.
David Papandrew
8,386 PointsHi Ryan,
You got the first part of the loop right and are incrementing the counter properly.
I think the only spot you got tripped up is on the sum. The exercise wants you to add the value of each item in the array to the sum. Your solution appears to be adding every item in the array the first time around.
With the solution below, the loop runs and grabs the first array item (at index 0) which is 2 and then adds it to the sum. The 2nd time through the loop, it grabs the item at index 1 (since counter is now at 1). So when we request the index item in the numbers array we ask for "numbers[counter]". That gets added to sum. So each time through the array the index position is changing.
Hope that makes sense.
while counter < numbers.count {
sum += numbers[counter] // This is the key part, as loop runs, counter is going from 0 to 1 to 2 until it hits the numbers.count limit
counter++
}
Ryan Maneo
4,342 PointsTechnically doesn't mine do the same thing, just with more code? In other words, it isn't shorthand?
David Papandrew
8,386 PointsYou could, but then you'd be left with superfluous code.
There's certainly other ways to do this. If you're looking for other ways to skin this cat, a for loop is even more succinct and you can dump the counter variable altogether:
for number in numbers {
sum += number
}
Ryan Maneo
4,342 PointsRyan Maneo
4,342 PointsRight, but what if I added a if condition which added a break statement to the while loop if sum is greater than the sum of numbers ran once? (It sounds silly, but would that technically do the same thing?)