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 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Logical Operators

Ethan Barrie
Ethan Barrie
2,628 Points

What is wrong with this code for the Logical Operators code challenge in Swift 2.0

Everything seems to be working properly in XCode so I'm not sure why it's giving me an error.

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

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

2 Answers

I think you can simplify your code a little bit:

Check to see if the number is odd and is a multiple of 7 (meaning if you divided the number by 7 the remainder would be 0)

if (n % 2 != 0 && n % 7 == 0){
    results.append(n) //for the numbers that fulfill this criteria, append them to the results array.
    }
Xavian Ang
Xavian Ang
1,242 Points

Hi Martin Granger ,

I was wondering why my code wouldn't work and when comparing it to yours, I've noticed that it only differs at the part where I'm adding values to my results variable

This are my codes

var results: [Int] = []

for n in 1...100 {
    // Enter your code below
    if( n%2 != 0 && n%7 == 0 ){
        results += [n] // I've replaced this with results.append(n) and it worked
    }
    // End code
}

Was wondering why mine didn't work and yours could.

Any thoughts?

Charlotte S
Charlotte S
829 Points

I also had this same problem - could anyone explain why results += [n] doesn't work but using results.append(n) does?

Barry Cherry
seal-mask
.a{fill-rule:evenodd;}techdegree
Barry Cherry
Front End Web Development Techdegree Student 3,395 Points

Can some one explain this exact code in full please? i only understand HALF of this, such as the if statements, how do you make swift find odd numbers?