How can I go through an array of odd numbers and add 5 to each instance?

Viewed 1336
var oddNumbers = [Int]()

for x in 0...100 where x % 2 != 0{
    oddNumbers.append(x)
}



var sums = [Any]()

for i in 0..<oddNumbers.count {
    sums = oddNumbers[i] + 5.0
}

I spent about an hour trying different things and can't seem to get it right. I've gotten like 3 different error messages but the one I get for this one is Cannot convert value of type Int to expected argument of type [Any].

5 Answers

According to newly released Swift 5 you can determine this using flatMap and isMultiple(of:) method

let sums = (0...100).flatMap { !$0.isMultiple(of: 2) ? Double($0) + 5.0 : nil }
Related