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 trial

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
PLUS
Dave Harker
Courses Plus Student 15,510 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: