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 trial

iOS Swift Collections and Control Flow Control Flow With Loops Working With Loops

axel trujillo
seal-mask
.a{fill-rule:evenodd;}techdegree
axel trujillo
iOS Development Techdegree Student 1,026 Points

cant pass this test, someone help me please

i dont get what am i getting wrong here

loops.swift
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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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 to sum
  • 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! :sparkles:

axel trujillo
seal-mask
.a{fill-rule:evenodd;}techdegree
axel trujillo
iOS Development Techdegree Student 1,026 Points

Hi, 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You 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 :sparkles:

axel trujillo
seal-mask
.a{fill-rule:evenodd;}techdegree
axel trujillo
iOS Development Techdegree Student 1,026 Points

got 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Sure! The line you're looking for is:

sum += numbers[counter]

There's simply no need for the variable in between :thumbsup: