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 trialFederico Blank
3,670 PointsStuck with sum = sum + newValue
I-m not sure how to place the code in this excersice, the first part is ok, but I not sure how to handle the newValue?
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.count {
print(numbers[sum])
counter++
}
while sum = sum + newValue {
print(sum[newValue])
counter++
}
5 Answers
Steve Hunter
57,712 PointsHi Frederico,
You need three lines for this challenge. Ignore the numbers[0], numbers[1]
etc as those were unnecessary additions that didn't add anything to the solution. Here's the key code:
while counter < numbers.count {
sum = sum + numbers[counter] // <- Key line; good code
counter++ // <- Key line; we'll loop forever without this!
}
The first sets up the loop to iterate through the contents of the array called numbers
. So, it uses counter
to keep track of its progress, incrementing it by one at each loop. As soon as counter
is equal to the number of elements in the array, numbers.count
, the loop stops.
Inside the loop, we add the current element to the previous ones, sum += numbers[counter]
. So, at the end of the looping, sum
contains the sum of all the elements in the array.
In the first loop, counter
is zero, sum
is zero, numbers[counter]
is 2. The value 2 gets added to sum
. The next loop counter
is 1, sum
is 2 and numbers[counter]
is 8 (the second element of the array. This carries on going round until all the elements have been added into sum
.
Make sense?
Steve.
Steven Deutsch
21,046 PointsHey Federico Blank,
First, you only need the first While Loop. You have the loop and the incremented set up properly, however, we need to replace the print statement with code that will add the values of the array to sum. Since counter is initialized to a value of 0, we can use this as an index value to access items in our array. For the first iteration of the loop, the index (counter) will be zero. After it adds that value to sum, counter will increment and then the loop will run again. This time the index (counter) will be 1. This process will continue until counter is greater than or equal to the number of items in the count array, therefore, adding up all the values into sum.
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++
}
Good Luck!
Steve Hunter
57,712 PointsHi Frederico,
Steven's answered that for you but if you want a longer explanation, have a look through this post that takes this challenge in small steps.
Steve.
Federico Blank
3,670 PointsThanks to both of you guys, I really appreciate your guidance. This is a new thing to me and some times it can be quite hard to understad. I've read both posts, and still can get the answer right. I really don't understad what should I wirte to replace numbers[0], numbers[1], etc from the following code.
while counter < numbers.count {
sum = sum + numbers[counter] // <- Key line; good code
numbers[0] // these don't add anything
numbers[1] // you can delete
numbers[2] // these lines
numbers[3] // all the way
numbers[4] // from the first
numbers[5] // right the way
numbers[6] // to here
counter++ // <- Key line; we'll loop forever without this!
}
From the other post I can deduct that:
var newValue += someArray[index]
could be:
var sum += number[counter]
But I'm not sure how to implement it. I'm starting to think that I'm not fit for this course...
Federico Blank
3,670 PointsHi there, I think I was drawining in a glass of water. I think I dind't understand the instructions. English is not my native language, and I'd been having issues with some explanations. Maybe I should go over the lessons again just to make sure...
thank you guys for your kind help!