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 trialMonzer Selim
Courses Plus Student 3,849 Pointshow can i write an if statement inside the loop that check if n is not even and multiple of 7
I need help with this challenge need to write an if statement inside the loop to check if n is not even and multiple of 7 and then append the results to the results array
var results: [Int] = []
for n in 1...100 {
if n !even && n >= 7 {
results.append(n)}
}
4 Answers
William Li
Courses Plus Student 26,868 PointsHi there.
the even you used in your code isn't a defined function/method/variable, so you can't use it; additionally, % operator is what we usually use to check for remainder.
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n%2==1 && n%7==0 { // check If the number is both an odd number and a multiple of 7
results.append(n)
}
// End code
}
hope it helps.
Jannung Kusdiantoro
6,743 Pointshow about this? for x in 1..100 { if (x%2 == 0) && (x/7 == 1) { print("number "+x+" is what i want") } }
Monzer Selim
Courses Plus Student 3,849 Pointsthanks a lot i totally forgot about the remainder
Jannung Kusdiantoro
6,743 Pointswe use x % 2 = 0 to check if x was even or odd number. if x % 2 == 0 then it was even, and if it return false, then it was Odd number. we check x / 7 == 1 to see if it was 7 multiplied number, combine both to get what you want. sorry for my bad english.
William Li
Courses Plus Student 26,868 PointsFYI, x/7==1
isn't the correct way to check for multiplier of 7, because only case when this equality comparison is true is when x == 7, otherwise false
Jannung Kusdiantoro
6,743 Pointssorry, i miss something. thanks for correction