I have a fixed array is: let terms: [Int] = [1,2,3,4,5,6,7,8,9,10,11,12,13,15,18,24,36].
I wrote a function with parameter is an Int. I want to check the parameter that will map with multiple condition in there function. That's my function:
"func configTerms(with term: Int) -> [String] {
termConditions = ["A"]
switch term {
case 1...:
termConditions.append("B")
case 2...:
termConditions.append("C")
case 6... where term % 3 == 0:
termConditions.append("D")
case 12... where term % 6 == 0:
termConditions.append("E")
case 24... where term % 12 == 0:
termConditions.append("F")
default:
print("Can't match any value")
}
return termConditions
}"
I can resolve it by using "if...else" control flow but the last condition will be very long and complex.
I used "switch...case" control flow but it couldn't return result that i wanted.
Example: When input was 12, function only returned ["A","B"] instead of ["A","B","C","D","E"]
Everytime the condition matched, the "switch...case" breaked out and only returned ["A","B"].
I used fallthrough but it didn't check the next condition and executed next expression.
How can i continue using "switch...case" in this case without using "if...else"?