0
0
Swiftprogramming~10 mins

Ternary conditional operator in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign the larger number to max using the ternary operator.

Swift
let max = [1] b > a ? b : a
Drag options to blanks, or click blank then click option'
A*
B+
C-
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using arithmetic operators instead of assignment.
Forgetting to assign the result to a variable.
2fill in blank
medium

Complete the code to print "Even" if number is even, otherwise "Odd" using the ternary operator.

Swift
print(number % 2 == 0 ? [1] : "Odd")
Drag options to blanks, or click blank then click option'
A"Odd"
B"Even"
C"Zero"
D"Number"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the true and false values.
Using incorrect strings.
3fill in blank
hard

Fix the error in the ternary expression to correctly assign the smaller value to min.

Swift
let min = [1] y < x ? y : x
Drag options to blanks, or click blank then click option'
A=
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using arithmetic operators instead of assignment.
Omitting the assignment operator.
4fill in blank
hard

Fill both blanks to create a ternary expression that assigns "Adult" if age is 18 or more, otherwise "Minor".

Swift
let status = age [1] 18 [2] "Adult" : "Minor"
Drag options to blanks, or click blank then click option'
A>=
B<
C?
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of operators.
Using wrong comparison operators.
5fill in blank
hard

Fill both blanks to create a ternary expression that assigns the absolute value of number to absValue.

Swift
let absValue = number [1] 0 [2] number : -number
Drag options to blanks, or click blank then click option'
A>
B?
C:
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of operators.
Using less than instead of greater than.