Churpy | OA | Ternary operator parser
Anonymous User
86

Create a parser that evaluates and executes ternary operations in the form of:

if ( <condition>, <truthy value>, <falsy value> )

Which would normally equate to:

if ( <condition> ) {
   return <truthy value>
} else {
   return <falsy value>
}

Sample input and output is as follows:

run(`
if (var_1 == 2, 0, if (var_2 == 4, 15, 0))
+ if (var_2 == 3, 5, 0)
- if (var_4 == 2, 0, 5)
+ if (var_3 == 3, 5, 0)`, {
  var_1: 1,
  var_2: 4,
  var_3: 3,
  var_4: 5
});

/*
 output : 15
*/

HINT: Recursion & Regular expressions may come in handy (extra points for minimal if’s)

Comments (0)