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 trialKjetil Korsveien
1,713 PointsLogic - not sure how to type it out
I know what to do, but how to I type it logically? Any hints?
var results: [Int] = []
for n in 1...100 {
if n != (n / 2) && n == (n / 7) {
results.append(n)
}
}
3 Answers
Jennifer Nordell
Treehouse TeacherI feel like you've confused division with the mod operator. The mod operator returns the remainder after a division. For exampe 5%2 will return a 1. After dividing 5 with 2 we get 2 with a remainder of 1. 2 times 2 is 4 and 4 subtracted from 5 is one. 7%2 will also give us a 1. So any odd number will have a remainder of one. Any even number will have a remainder of 0.
If a number divided by any other number has a remainder of 0 we say that that number is evenly divisible by the other. So 14 is evenly divisible by 7. The division sign as you've used will have a result of 2 while the mod will have a result of 0. We get a result of 2 with a remainder of 0.
We want all numbers that are evenly divisible by 7 and all odd numbers appended to the results array. Take a look:
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if(n % 2 != 0 && n % 7 == 0){ //if the number is odd AND evenly divisible by 7
results.append(n)
}
// End code
}
Hope this helps!
garyguermantokman
21,097 PointsHey remember to use the remainder operator % Link to Swift Operators
if n % 7 == 0 && n % 2 != 0 {
results.append(n)
}
Kjetil Korsveien
1,713 PointsTnx Mates. Iยดve finally worked it out. I had the code right at last with the exception of my use of parentheses. Coding can be so hard sometimes. My attention to detail isn't of the greatest. :)