0
0
Javascriptprogramming~10 mins

Ternary operator in Javascript - 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 'adult' if age is 18 or more, otherwise 'minor'.

Javascript
const status = age [1] 18 ? 'adult' : 'minor';
Drag options to blanks, or click blank then click option'
A>=
B==
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>=' causes wrong classification.
Using '<' reverses the logic.
2fill in blank
medium

Complete the code to assign 'even' if number is divisible by 2, else 'odd'.

Javascript
const type = (num % 2) [1] 0 ? 'even' : 'odd';
Drag options to blanks, or click blank then click option'
A>
B<
C===
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' causes odd numbers to be classified as even.
Using '>' or '<' is incorrect for equality check.
3fill in blank
hard

Fix the error in the ternary condition to check if score is passing (>= 60).

Javascript
const result = score [1] 60 ? 'pass' : 'fail';
Drag options to blanks, or click blank then click option'
A!=
B<
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the pass/fail logic.
Using '==' only passes if score is exactly 60.
4fill in blank
hard

Fill both blanks to create a ternary that returns 'positive' if num > 0, else 'non-positive'.

Javascript
const sign = num [1] 0 ? 'positive' : [2];
Drag options to blanks, or click blank then click option'
A>
B'non-positive'
C<=
D'negative'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in first blank reverses the logic.
Using 'negative' instead of 'non-positive' changes meaning.
5fill in blank
hard

Fill all three blanks to create a ternary that returns uppercase key if value > 10, else lowercase key if value > 5, else 'low'.

Javascript
const label = value [1] 10 ? key.[2]() : (value [3] 5 ? key.toLowerCase() : 'low');
Drag options to blanks, or click blank then click option'
A>
BtoUpperCase
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>' changes the logic.
Using 'toLowerCase' in second blank returns lowercase instead of uppercase.