iOS Swift Collections and Control Flow Control Flow With Conditional Statements Working With Logical Operators

David Sabzanov
David Sabzanov
2,430 Points

Got and error that say to make sure I'm appending the proper value but my code seems correct. What am I doing wrong?

I've tested this code in Xcode in a Playground and it seems to work. So what's the problem?

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

for n in 1...100 {
    // Enter your code below
    if n%7 == 0 && n%2 != 0 {
        results += [n]
    }
    // End code 
}
David Sabzanov
David Sabzanov
2,430 Points

This is the exact error I got: Bummer: Make sure you're appending the correct values to the results array in order to pass the challenge

1 Answer

Magnus Hållberg
Magnus Hållberg
16,951 Points

Theses challenges are very picky about the details, all that's wrong is that's its in the wrong order. First you try if its odd and then if its a multiple of 7. You have done the other way around.

David Sabzanov
David Sabzanov
2,430 Points

I don't think it matters if I check odd first and then if its a multiple of 7 because I use the && operator. So both conditions must be true.

But I was able to solve this issue. The mistake was using "results += [n]" to append the integer to the array. It should be correct but the online compiler didn't accept it. So I changed the code to use ".append" method and it worked!

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

Thank you for your help!