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

charles wilson
charles wilson
8,506 Points

a) what should end up in print() b) should this utilize the && logical operator type

I'm not sure what this should look like to be completely honest. A little lost.

operators.swift
var results: [Int] = [7,21,35,49,63,77,91]

for n in 1...100 {
    // Enter your code below
    if n != [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,
    84,86,88,90,92,94,96,98,100] && n = [7,21,35,49,63,77,91] {
    print("[n] is a multiple of seven")
    }
    // End code 
}

1 Answer

Sam Donald
Sam Donald
36,305 Points

Ok, first results should be an empty array

var results: [Int] = []

When n is both an odd number and a multiple of 7 we are going to add it to results.

Remainder(%)

This operator returns the "remainder" left over when one operand is divided by a second. So when we want to check if n is odd, we would want to know if the remainder was not(!) 0.

n % 2 != 0

To check if n is a multiple of seven we just replace 2 with 7 and lose the not checker.

n % 7 == 0

Putting it all together we check for oddness and multiple(ness?) then add n to results if it passes.

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

print()

You don't need it. All they want you to do is add(append) n to results.

charles wilson
charles wilson
8,506 Points

Thank you so much Sam! Helped loads. Maybe I'm too new (or dumb) to have realized right off to use the remainder operator.

I also didn't think to use the append method... Looks like I'll be begging for help lots more in the future.