iOS Swift Collections and Control Flow Control Flow With Conditional Statements Working With Logical Operators

whats the error

whats wrong with my code on this challenge ?

operators.swift
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
MOD
Alex Koumparos
Python Web Development Treehouse Moderator 32,453 Points

Hi 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