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 trialWilliam Hartvedt Skogstad
5,857 PointsChallenge Task 1 of 1 For this challenge, we'd like to know in a range of values from 1 to 100, how many numbers ...
Challenge Task 1 of 1
For this challenge, we'd like to know in a range of values from 1 to 100, how many numbers are both odd, and a multiple of 7.
To start us off, I've written a for loop to iterate over the desired range of values and named the local constant n. Your job is to write an if statement inside the for loop to carry out the desired checks.
If the number is indeed both an odd number and a multiple of 7, append the value to the results array provided.
Hint: To check for an odd number you can either use the remainder operator and 3, or use the not operator to check for "not even"
I need help with how to check if a number is a multiple of 7.
var results: [Int] = []
for n in 1...100 {
if n == n % 3 = 0 && n == (7,14,21,28,35,42,49,56,63,70) {
results.append(n)
}
}
9 Answers
Farhad Mohammad Afzali
5,007 PointsHello, you need to put your conditions in a formula rather than hard coding them. Please check my solution below.
var results: [Int] = []
for n in 1...100 { // Enter your code below if (n % 7 == 0 && n % 2 != 0) { results.append(n) } // End code }
Hope it helps
Michael Dussie
1,891 Pointsvar results: [Int] = []
for n in 1...100 {
// Enter your code below
if (n % 7 == 0 && n % 2 != 0) {
results.append(n)
}
// End code
}
Sapho Maqhwazima
805 PointsI know I'm missing something, but can anyone tell me why we do we type n % 7 == 0. I understand the n % 7 part, whats the story with the 0? Can someone please break this down for me.
Grayson Hary
5,033 PointsAddressing Sapho's question about the 0.
n % 7 == 0
As you know, % is our remainder operator, so we're seeing how many times 7 fits into n, and if that remainder is anything other than 0, we know it is not a multiple of 7.
Farhad Mohammad Afzali
5,007 PointsAnytime. The best way to check if a number is odd or even is to check it remainder dividing it by 2. n%3 == 0 will not work because, for example, 11 is odd but not divisible by 3, so it will return false. Thanks
William Hartvedt Skogstad
5,857 PointsSo they were misleading with the use of the number "3" to check for odd numbers in this challenge?
Farhad Mohammad Afzali
5,007 PointsI think so
William Hartvedt Skogstad
5,857 PointsAlright! Thanks for all your time and help!
Farhad Mohammad Afzali
5,007 PointsYou are welcome :)
Rich Braymiller
7,119 Pointsi din't get what the remainder does?? Sorry if this is a stupid question....
Shreehari Aravind
809 PointsIf you use the function 7 % 2, the reminder will be 1.
The remainder function gives you the remainder when you divide one number by another.
richard barnes
2,949 Pointsvar results: [Int] = []
for n in 1...100 {
if (n % 7 && ! % 2) == 0 {
results += n
}
}
just out of interest, is this also a correct way of performing the task. crashed the challenge though?
Seth Hamumu
iOS Development with Swift Techdegree Student 836 Pointsvar results: [Int] = []
for n in 1...100 { // Enter your code below if (n % 7 == 0 && n % 2 != 0) { results.append(n) } // End code }
var ree: [Int] = []
for n in 1...100 { // Enter your code below if (n % 7 == 0 && n % 2 != 0) { ree += [n] } // End code }
These are the same code. Just know they both output the same result if you run it in Swift Playground but for some reason Treehouse compiler seem to show that the second method is incorrect.
Kellington Chidza
866 Pointsif (n % 7 == 0 && n % 2 != 0) { results.append(n) }
William Hartvedt Skogstad
5,857 PointsWilliam Hartvedt Skogstad
5,857 PointsThanks a lot for you help! One more question tho. The code challenge didn't work with the remainder operator using 3 like this:
if (n % 7 == 0 && n % 3 == 0)
Why not?
Ricky Lim
2,537 PointsRicky Lim
2,537 PointsI used "results += [n]" instead of "results.append(n)" but it didn't work.
Is there something wrong with using "results += [n]" for this task?
Linus Karlsson
7,402 PointsLinus Karlsson
7,402 PointsI understand your code, but I had first worked on a different solution:
Can you or someone else explain to me why my version is faulty, and also how I should know to use the % (modulo) to complete the challenge when it wasn't even mentioned in the preceding video?
These Swift challenges seems unnecessarily difficult to a beginner...