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 trialJohn Ajayi
6,669 PointsFor in Loops Challenge
Hi Can anyone help on the For In Loops Challenge???
I completed it but when i did it in Xcode it comes up with an Error.
The Challenge task is below.
For this challenge, we're going to use a for in loop to compute the multiplication table for 6 and append the results to an array.
To start off, I've created an empty array of type Int and assigned it to a variable named results. Note that we have to explicitly declare the type here so that the compiler knows this array will hold Int values.
Your task for this step is to create a for in loop that iterates over a range. Multiplication tables typically range from 1 to 10 (with 10 included) so the range you are iterating over should go from 1 to 10.
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.
2 Answers
M T
7,668 PointsCould you post the code you used to complete the challenge?
John Ajayi
6,669 PointsOk Apologies
Step 1 asked the following:
For this challenge, we're going to use a for in loop to compute the multiplication table for 6 and append the results to an array.
To start off, I've created an empty array of type Int and assigned it to a variable named results. Note that we have to explicitly declare the type here so that the compiler knows this array will hold Int values.
Your task for this step is to create a for in loop that iterates over a range. Multiplication tables typically range from 1 to 10 (with 10 included) so the range you are iterating over should go from 1 to 10.
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.
The code i put was:
var results: [Int] = [6]
for multiplier in 1...10 {print("(multiplier) times (results) is equal to (multiplier) * (results)")}
The Error:
in the Debug area the output shows:
1 times [6] is equal to 1 * [6] 2 times [6] is equal to 2 * [6] 3 times [6] is equal to 3 * [6] 4 times [6] is equal to 4 * [6] 5 times [6] is equal to 5 * [6] 6 times [6] is equal to 6 * [6] 7 times [6] is equal to 7 * [6] 8 times [6] is equal to 8 * [6] 9 times [6] is equal to 9 * [6] 10 times [6] is equal to 10 * [6]
which is incorrect. I think it has something to do with the [Int] but this was already given in the challenge so i do not believe i should be changing it. but when i changed it in Xcode (removing [Int] and leaving 6 with no []) it worked.
The second problem was with challenge number 2:
Inside the body of the loop, we're going to use the multiplier to get the multiple of 6. For example, if the multiplier is 1, then the multiple is 1 times 6, which is equal to 6. β¨Once you have a value, append it to the results array. This way once the for loop has iterated over the entire range, the array will contain the first 10 multiples of 6.
The code i used was:
var results: [Int] = [6, 12, 18, 24, 30, 36, 42, 48, 54, 60]
for multiplier in 1...10 {print("(multiplier) times (results) is equal to (multiplier) * (results)")}
It worked and i completed the challenge but in Xcode it show up the same as it did in the Debug area above but with the additional numbers
hope this make sense
Thanks for your help
ahyuursagv
18,371 PointsOn part 2 of 2 this worked for me. It might have just been me but the wording in the challenge seemed slightly confusing so I had trouble understanding what was expected too at first. Hope this helps.
var results: [Int] = []
for multiplier in 1...10 {
results.append(multiplier * 6)
}
Martin Wildfeuer
Courses Plus Student 11,071 PointsMartin Wildfeuer
Courses Plus Student 11,071 PointsPlease update this post with your code, so we can go through it. Moreover, please let us know what the Xcode error is. Thanks!