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

Ken Gerlinger
Ken Gerlinger
880 Points

why does if statement not work?

The number has to be odd and must be a multiplier of 7. How do insert those numbers in an array ?

operators.swift
var results: [Int] = []

for n in 1...100 {
    // Enter your code below
   if n%2!=0 && n%7==0{ results.append(n)}
    // End code 
}

1 Answer

Matthew Long
Matthew Long
24,413 Points

"It does." But you need some spaces between some operators. Without the spaces various errors will display in Xcode. For example, it thinks the ! operator is being used to force unwrap something that isn't even an optional when there is no space.

var results: [Int] = []

for n in 1...100 {
  if n%2 != 0 && n%7 == 0 {results.append(n)}
}