The task was to take an array of numbers and parse it
- remove even numbers
- sort asc
- output: {num} is a lucky number for each number
- NO temp variables
Here’s my attempt, it works but I’m not sure its 100% correct
var ln = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
func oddNumbers(data: [Int]) -> [Int] {
data.filter { !$0.isMultiple(of: 2) }
}
func sortedData(data: [Int]) -> [Int] {
data.sorted { $0 < $1 }
}
func printNumbers(data: [Int]) -> Void {
for i in data {
print("\(i) is a lucky number")
}
}
func doNumberStuff(data: [Int], using getOddNums: ([Int]) -> [Int]) -> Void {
printNumbers(data: sortedData(data: getOddNums(data)))
}
doNumberStuff(data: ln, using: oddNumbers)
I had a thought about passing in an array of function references to using then iterating over that array within doNumberStuff
.
Maybe I’ll try that at a later date.
It was mentioned to chain the functions as a hint but I’m not familiar with this syntax. Maybe it was missed.
To chain these functions, use luckyNumbers.first { }.second { }, obviously putting the real function calls in there.
Leaving this note so I remember to update this at a later date.