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 trialaxel trujillo
iOS Development Techdegree Student 1,026 Pointscant pass this test, someone help me please
i dont get what am i getting wrong here
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter <= numbers.count {
counter += 1
var newValue = counter
sum += counter
}
2 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyou are close, just need a few tweaks. the first thing to realize is that with 0-indexed arrays, the index of the last element is the length of the array minus 1. this means that if your while loop runs until counter is equal to numbers.count, you will actually overrun the array, because there is no index 7 in an array with 7 elements. 6 is the last one. so you can just use < instead of <=. secondly, you don't really need to create another variable just to assign the value of counter to it. you can use counter. what you use it for is to index into the array to retrieve its values, and add them to the sum variable. you use the square brackets [] as in myArray[index]. counter is your index. so what you add to sum isn't counter, it's the value in the array at [counter] index. finally, instead of incrementing counter at the beginning of the loop, it needs to be the last thing the loop does. since counter starts at 0, 0 being the first index of an array, if you immediately increment it to 1, you will skip the first element in the array when you add to sum.
Jennifer Nordell
Treehouse TeacherHi there! As James has noted, you're actually really close here so I'm going to summarize a bit and leave a few hints:
- The
newValue
variable is unnecessary and can be safely removed - You are saying while the counter is less than or equal to count.
- Count will be equal to 7 as it is the number of items
- The maximum index will only be 6 as this is a "0 indexed" array
- You need to get the number at the position of the value of
counter
from the array and add it tosum
- counter should be incremented last, otherwise you will skip the first element as counter will be 1 and the item retrieved will be 8
Hope this helps!
axel trujillo
iOS Development Techdegree Student 1,026 PointsHi, can you help me with the code? i dont get how can i use newValue without creating a variable. also thank you so much for your answer!
Jennifer Nordell
Treehouse TeacherYou don't have to use newValue
at all. It's there as an example. You only need two lines of code inside your while
loop
axel trujillo
iOS Development Techdegree Student 1,026 Pointsgot it right like this:
while counter < 7 { var newValue = numbers[counter] sum += newValue counter += 1 }
but can you please tell me how to do it without the var newValue?
Jennifer Nordell
Treehouse TeacherSure! The line you're looking for is:
sum += numbers[counter]
There's simply no need for the variable in between
axel trujillo
iOS Development Techdegree Student 1,026 Pointsi really appreciate your help, thank you so much!
axel trujillo
iOS Development Techdegree Student 1,026 Pointsaxel trujillo
iOS Development Techdegree Student 1,026 Pointsthank you james, huge help :)