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

mark hengstebeck
mark hengstebeck
1,119 Points

Working with loops: challenge task 1 of 2: How to get from here to challenge task 2 of 2?

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[counter]) counter += 1 }

loops.swift
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
var newValue = 

// Enter your code below
 while counter < numbers.count{
    print(numbers[counter])
    counter += 1 
    sum += index counter
    }

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Because I feel like you have mostly the right idea, I'm going to give some hints here as I think it will be a better overall learning experience for you.

  • You do not need to set up a new variable such as you've done with newValue
  • Nothing needs to be printed. You may remove the print statement.
  • We use counter to hold our place through the loop
  • Access the element of the numbers array using the index of counter such as numbers[counter]
  • Add the number in the array to the current running total of sum
  • increment counter after adding the number to sum... not before

I hope this helps, but let me know if you're still stuck! :sparkles:

mark hengstebeck
mark hengstebeck
1,119 Points

Hi Jennifer, That's it! Thanks, Mark