Heads or Tails explainer

Aim of the puzzle: Use the ternary operator in place of an if…else statement.
Walk through of solution:

To review, an if…else statement takes this format:

if (thisIsTrue) {
} else {

A ternary operator replaces this format with something much shorter:

isThisTrue ? doThis : doThisInstead

The code before the ? is called the test. If the test returns ‘true’, then the code after the ? will execute. If the test returns false, then the code after the : will execute. Ternary operators are useful in how they use fewer lines of code than an if…else statement, however they can be less readable by humans.

The coin variable uses the pickRandom() function to choose randomly between the strings 'heads' and 'tails'. This variable will then be used in our ternary operator.

A completed ternary operator is provided as an example. It will test if the variable coin has the value 'heads'. If this is true, then drawBoxes('rwr rrr rwr') will execute. If the coin variable is 'tails', then the test will return false, and drawBoxes('bbb wbw wbw') will execute.

To complete this puzzle, add a second ternary operator. The test will be the same, but this time we will print('heads') to the console if the test coin === 'heads' is true, or print('tails') to the console if the test is false.

You can also complete the puzzle by testing if coin === 'tails', as long as the correct print statement is also used.

Sample code solution:

var coin = pickRandom([

coin === 'heads' ? drawBoxes('rwr rrr rwr') : drawBoxes('bbb wbw wbw');
coin === 'heads' ? print('heads') : print('tails'); 

Javascript Concepts: Ternary Operator, If Statements, Functions, Arrays
Grasshopper Concepts: print(), pickRandom()