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 trialEvgenii Kireev
717 PointsI'm not understanding what i do wrong ?
Hello! Please can you check my task what i'm doing wrong: Task: 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 both an odd number and a multiple of 7, append the value to the results array provided.
Hint: To check for an odd number use the not operator to check for "not even" https://teamtreehouse.com/library/swift-collections-and-control-flow/control-flow-with-conditional-statements/working-with-logical-operators
This is my code:
var results: [Int] = []
for n in 1...100 {
if n % 2 != 0 || n % 7 == 0 {
print(n)
results.append(Int(n))
} else {
}
}
Bummer! Make sure you're using the logical AND operator to perform two sets of check
6 Answers
Mitch Little
11,870 PointsEvgenni,
There should only be 7 cycles because there are only 7 values between 1 and 100 that are BOTH, odd AND a multiple of 7.
These numbers are: [7, 21, 35, 49, 63, 77, 91].
If you could show me your solution so far we can see if something else isn't compiling correctly ?
Mitch Little
11,870 PointsHi Evgenii,
You are almost there!
Firstly, you have used an OR operator ||. The AND operator is: &&
Secondly, you don't have to print 'n' but simply append it to the results array like you have attempted.
In your append method, you do not need to specify that 'n' is an Integer type in the append method which I believe you have tried to do!
For an example:
array.append(/* Value To Append Goes Here */)
Also, an else statement isn't required to pass the challenge.
I hope you can work it out from here however if not, please feel free to ask any more questions!
All the best,
Mitch
Elena Iliasova
iOS Development with Swift Techdegree Student 1,739 PointsHello Mitch. Help me please. I don't understand this task((Why do we use 2? Why did we label it as an odd chilo? and the expression n% 7 == 0 means that the cycle (1 ... 100) must be a multiple of 7?
Evgenii Kireev
717 PointsHello Mitch! Thank you for the answer. Anyway i can not pass the task, because the program tells me: Make sure you're using the logical AND operator to perform two sets of checks. But i don't know how create the sets of check with operator AND &&, instead of operator OR ||.
Mitch Little
11,870 PointsHi Evgenii,
You have used the OR Operator in your solution.
Your if statement performs the two sets of checks. It is essentially saying 'if n is odd OR '||' n is a multiple of 7, then do this...'
if n % 2 != 0 || n % 7 == 0 {}
You want to say 'if n is even AND '&&' n is a multiple of 7, then do this...'
if n % 2 != 0 && n % 7 == 0 {}
Any operator you use goes in-between the two or more things you want to compare.
You've got this!
Mitch
Evgenii Kireev
717 PointsThank you so much Mitch! But it is not correct too. Because there is only 7 cycles with the operator AND &&. But in my code its 57, that a count of values need to put to the array: -Both odd [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] -Multiple of 7 [14, 28, 42, 56, 70, 84, 98] Total: 57 value
Evgenii Kireev
717 PointsThank you so much for help Mitch! I've got it! You were right. i understood the question wrong.
Mitch Little
11,870 PointsYou're very welcome!
All the best,
Mitch