Problem Description
Let's write a function getAccessLevel(role) that takes a role string.
If the
roleis"admin", return"Full Access".If the
roleis"editor", return"Limited Access (Editing)".For any other role (including
undefinedor""), return"Guest Access".
Constraint: You must solve this with a single return statement using only ternary operators. No if/else.
Test Cases:
getAccessLevel("admin")should return"Full Access"getAccessLevel("editor")should return"Limited Access (Editing)"getAccessLevel("user")should return"Guest Access"getAccessLevel("")should return"Guest Access"
馃挕 The 'Syntax Nudge'
This is what separates a novice from a master. The valueIfFalse part of a ternary operator doesn't just have to be a simple value... it can be another ternary operator.
Think about it like this: return (condition1) ? "valueA" : (condition2 ? "valueB" : "valueC")
How can you "nest" your logic to check for "admin", then "editor", then everything else?