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

Chris Shaffer
Chris Shaffer
12,030 Points

Does not accept a valid answer. I tested this in my own Xcode compiler and it works fine, yet I get an error.

I've tested this in Xcode and it works fine (because it's valid math) yet it isn't accepted as the answer.

It's unclear what else you could possibly be looking for.

var results: [Int] = [] // print all numbers 1-100 that are both odd and multiples of 7. for n in 1...100 { if n % 2 != 0 && n % 7 == 0 { print(n) results.append(n) } }

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 
}
Chris Shaffer
Chris Shaffer
12,030 Points

n % 2 !=0 // checks to see if the number is evenly divisible by 2 (i.e. "odd") n % 7 == 0 // checks to see if the number is a multiple of 7 (divisible by 7 w/no remainder)

2 Answers

Strange, all of the following worked when I tried it. Just make sure you have a space between != and 0.

var results: [Int] = []

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

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

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

Yep, I figured it out. Silly mistake - I was missing a space between the != and 0.