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

Dean Kreseski
Dean Kreseski
2,418 Points

Anybody can help on this one, still not sure about the wording of the question.

I understand the if statements and the operators I think its putting it all together. And also what is a good use for this in a app?

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

for n in 1...100 {
    // Enter your code below

 if (n % 7 == 0 && 5 & 2 != 0)

    // End code 
}
Nathan Fulkerson
Nathan Fulkerson
iOS Development with Swift Techdegree Student 28,943 Points

It looks like you solved the problem, but to answer your second question--what use does this have for apps--this is a very common programming puzzle often called "FizzBuzz." Chances are if you interview for an app developer job, you might end up having to solve this problem.

Realistically speaking, you might use some code like this so that every other table view cell is a different color, or something like that. A problem like this is a good way of simply learning Swift syntax, like loops, equality checking, conditionals, comparators, etc.

3 Answers

Maria Te
Maria Te
8,365 Points

Well, the challenge is asking you to append n to results only if n is divisible by 7 and an odd number. And anyway, the code that you tried isn't even correct. Your code should look like this actually:

var results: [Int] = []

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

Hope this helped! :-D

Dean Kreseski
Dean Kreseski
2,418 Points

I actually got it figured out right after I posted this. but thanks for your help.

Maria Te
Maria Te
8,365 Points

Got it, just ask anyway!

Latrell Garnett
Latrell Garnett
2,840 Points

Hi,

I'm not understanding this one. I don't understand the operators logic in the if line of code (n % 7 == 0 && n % 2 != 0) how does it check for odd numbers?

Maria Te
Maria Te
8,365 Points

well the part that says n%2 != 0, then that means n is not divisible by two. And only odd numbers aren't divisible by 2. so I guess that's your answer