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

Christos Xenophontos
Christos Xenophontos
6,520 Points

Is this not correct?

var results: [Int] = []

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

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

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

1 Answer

Dave Harker
PRO
Dave Harker
Pro Student 15,507 Points

Hi Christos Xenophontos,

A couple of small errors.
You are checking if the number (n) is even (if n%2 == 0) when you should be checking if odd (if n%2 != 0)
Also, should be appending to the array ... Something like:

if (n%7 == 0 && n%2 != 0) { // not equals 0 rather than equals 0
  results.append(n) // appending, as per challenge requirement
}

FYI: If you want to use the += notation outside of the challenge environment you'd need to add an array to the existing array, something like:

results += [n]

That won't pass the challenge, but works outside of the challenge environment. I'd suggest sticking with 'best practice' and using the inbuilt swift methods -> array.append(value)

Really good effort though :smile: Keep practicing, and happy coding.
Dave :dizzy: