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
erik Jones Oniga
3,307 PointsSubject(Swift) Can somebody please tell me how to append a for (name ) in to an array
Swift Loop (Range operator)
erik Jones Oniga
3,307 Pointscan you tell me the syntax of that?
erik Jones Oniga
3,307 Pointsyeah that's what i mean
jcorum
71,830 Pointserik, sorry, but I don't know what you are referring to? syntax of what?
erik Jones Oniga
3,307 Points4 Answers
jcorum
71,830 Pointserik, I see I guessed wrong.
// Enter your code below
var results: [Int] = []
let multiplier = 6 //create constant
for i in 1...10 { //loop from 1 to 10 using a range
results.append(i * multiplier) //append the product of each number in the range and multiplier
}
jcorum
71,830 PointsWell, I'm guessing here, but this may be what you are interested in:
var nums = [Int]()
for n in 1...10 {
nums.append(n)
}
nums // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
First we create an array of Ints named nums. Then we loop through the range 1...10, and each time we append the value to the array.
erik Jones Oniga
3,307 Pointsthank you very much
erik Jones Oniga
3,307 Pointsbut i did the same thing without the constant let multiplier = 6 and i did (for i in 1...10{ results.append(multiplier * 6)}
and it was right, is that ok?
jcorum
71,830 Pointserik, the editor can be really picky sometimes, and sometimes not. You are correct. I didn't read the directions carefully enough. They do say "For in loops also define a constant that temporarily stores the value in the iteration process. For the loop you're writing, name this constant multiplier." Interesting that the editor didn't complain.
I don't like hard-coding numbers in functions, loops, etc. So I created the separate constant by habit!
jcorum
71,830 Pointsjcorum
71,830 Pointserik, can you give us a link to the challenge, or a copy of the code? Are you talking about doing the append inside a for loop?