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

Xavian Ang
Xavian Ang
1,242 Points

My answer isn't getting accepted and I don't know why

The task is to list down all numbers from 1 to 100 that are both odd numbers and are a multiple of 7.

This is my solution

var results: [Int] = []

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

Am I doing something wrong here?

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

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

2 Answers

Piotr Nejman
Piotr Nejman
3,374 Points

Try to use append instead of +=

Xavian Ang
Xavian Ang
1,242 Points

I've got it to work using append. But doesn't += do the same trick as append in this case? Just curious.

Piotr Nejman
Piotr Nejman
3,374 Points

"You had a couple of options for appending an element to an array. In early betas, you could use the += operator to append an individual element, but this is no longer the case. You can however still use the append function." http://stackoverflow.com/questions/24002733/add-an-element-to-an-array-in-swift