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 trialRonald James
Courses Plus Student 463 Pointswhats the error
whats wrong with my code on this challenge ?
var results: [Int] = [7,21,35,49,63,77,91]
var even: [Int] = [14,28,42,56,70,94]
for n in 1...100 {
// Enter your code below
if Int == even == !results
// End code
}
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Ronald,
In Swift you cannot chain equality operators together in the way you have done. If you want to perform that kind of comparison, you need to use the &&
(AND) or ||
(OR) operators.
So, if you wanted to check that some value x
was equal to a
and also not equal to b
, you would write:
if x == a && x != b {
// do something
}
You're also comparing if an instance of a type (even) is equal to the type itself (Int), which will always be false. Remember that the general approach for checking if some Int can be divided evenly into another Int, we use the modulo (%
) operator.
For example, we can test if a
is divisible by 3 by asking:
if a % 3 == 0 {
// do something
}
And of course, checking if a number is even is just a special case of the general checking if a number is a multiple of another number, in this case, checking if the number is a multiple of 2.
You're also putting the !
in the wrong place. When you want to check if a value is not equal to another value, you modify the equality operator, so ==
becomes !=
.
Hope that clears everything up for you,
Cheers
Alex