Round 2 of 100 days of code starts today and im working with lessons on Treehouse. The last section I was working on was Enums and optionals before starting over again. I have a bad tendency to start from scratch after I stop being able to tell what every line of code does. Starting from scratch on the Swift beginners course and working through it im back on the control flow section working with while loops.

While conditions are good if you want something to run consistently till the conditions you give are false. Its one of the more true to normal english pieces of writing code. For this code challenge there is an array and I must compute the sum. Pretty simple task, what I got stuck on is having each item in the array be put in the sum variable array and added up.

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

while counter < numbers.count {

counter += 1

The key to pulling out every item in the array is using a counter. So to add them all up I have to use the other variable counter.

sum = sum + numbers[counter]

Writing out counter in array brackets enable each value in the numbers array to be added to the sum variable.

sum = 0 + 2

sum = 2 + 8

sum = 10 + 1

Once this is done the problems solved and you just print out the sum at the end.

while counter < numbers.count {

sum = sum + numbers[counter]

counter += 1


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s