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 2.0 Collections and Control Flow Control Flow With Loops For In Loops

Ali Al-Bahrani
Ali Al-Bahrani
17,584 Points

for loop challenge: trying to solve the problem but no luck till now , i need help

i was trying to find the solution for the code challenge but no luck , please help

loops.swift
// Enter your code below
var results: [Int] = []

5 Answers

Ollie King
Ollie King
10,194 Points

Hi Ali,

I managed to find a shorter way to how you applied the for loop to the array:

var results: [Int] = []

for multiplier in 1...10 { results.append/This is appending the array straight from the loop, no need for the var result/(multiplier * 6)/this is the multiplication loop/ }

Hope this helps :)

Ali Al-Bahrani
Ali Al-Bahrani
17,584 Points

sorry i just figured out the solution , thanks

var results: [Int] = [] var count = 0; var result = 0 for multiplyer in 1...10 { result = multiplyer * 6 results.append(result)

}

Ollie King
Ollie King
10,194 Points

Hi,

Sorry if it's simple, but I'm not understanding how the code you're supplying works.. Would you be able to explain further? (I'm having similar difficulties)

Cheers!

Ali Al-Bahrani
Ali Al-Bahrani
17,584 Points

// this is the array that you will save the answers of the multiplier var results: [Int] = []

//here is local variable to store the the answer of the for loop
var result = 0 //here i did for loop to go through from 1 to 10 times for multiplyer in 1...10 { //in this step i did do the multiplication

result = multiplyer * 6 // in this step i did append to store the result into results array results.append(result) }

Ollie King
Ollie King
10,194 Points

Ah ok, is there not a way of cutting out the "var result = 0" as I thought the name "multiplier" worked as the temp "var" to store the results of the for loop as it is the for loop.. I know you're saying that it's there to store the answers of the for loop.. maybe that's where I was going wrong.

Thank you for your help Ali!

Ali Al-Bahrani
Ali Al-Bahrani
17,584 Points

good thanks for sharing the code Ollie