Flip a Coin Again explainer


#1

The aim of this puzzle: Use an if-else statement to simplify the Flip a Coin puzzle.

Walkthrough of the solution: This puzzle starts out very similar to the original Flip a Coin puzzle, but instead of 2 if statements, there’s an if-else statement.

An if-else statement is really just a regular if statement with another set of curly brackets. That second block {} of code will only run if the code in the if statement parentheses is false. So, the if statement checks the parentheses and if it’s true the first code block runs, and if it’s false the second code block runs. Only one will run, not both or neither.

In this puzzle, there are only two categories that the answer can be: heads or tails. If it’s not ‘heads’ then it must be ‘tails’. That means we can just check for ‘heads’ in the first if statement and if it doesn’t pass the test, the else block will run and we know the answer is ‘tails’. So in the else block, we just need to add a drawBoxes() call with a string.

Sample code solution:
(Tap below to reveal)

var answer = pickRandom([
  'heads',
  'tails'
]);
print(answer);
if (answer === 'heads') {
  drawBoxes('gwg ggg gwg');
} else {
  drawBoxes('rrr wrw wrw');
}

JavaScript Concepts: Binary Expression (===), Calling Functions, Conditionals (if-else statement), Data Structures (Arrays), Identifiers, Variable Declaration
Grasshopper Concepts: pickRandom(), print()


#2

I was doing it right, wondered why my answer is not passing, checked here, seems like a bug in the app!


#3

Hey @Sudhanshusingh,

Would you be able to share a screenshot of your code so we can investigate further? Or share it with us via the ‘Report an issue’ option, available from the three dot menu on the top right corner?

Thanks!
H :slight_smile:


#5

Q: this is my road block as far as learning. The syntac is what gives me trouble
Thanks
B


#7

Seems like I’m doing it right as well, just can’t see where I’m going wrong. It draws the tails image correctly, and it’s randomized, just not letting me pass.


#8

Hey @Spacegras,

Can you share a screenshot of your code? That way I can have a look and see what’s going wrong.

H :slight_smile:


#9


#10

Hey @Spacegras,

You’ve added an extra if statement, inside the else block. That’s not necessary to solve this puzzle.

Try removing that if statement, then adding just drawBoxes() inside the else block {}.

If you’re stuck in the future, you can reveal the answer to a puzzle by tapping on the blurred section in the explainer.

H :slight_smile:


#11

U don’t need to put it only just put drawbox() the string.


#12

I don’t yet grasp how/why the colors were changed. Where did the red come from? Please explain! Thanks.


#13

Hey @123,

Do you want to share a screenshot of your code and I can walk you through whats happening?

H :slight_smile:


#14
  1. Thanks!
  2. How do I add a screenshot here?

#15


#16

The drawBoxes() function is a shortcut way to call a lot of drawBox() and newLine() functions.

drawBoxes('rrr ggg') will transform into:

drawBox(red);
drawBox(red);
drawBox(red);
newLine();
drawBox(green);
drawBox(green);
drawBox(green);

because each:

  • 'r' turns into drawBox(red)
  • 'g' turns into drawBox(green)
  • ' ' (a space) turns into newLine()

Let me know if this doesn’t answer your question.

–Frankie


#17

!
Am I doing somethung wrong?


#18

There are 2 main changes to make:

  1. Nested Ifs

    • When you put an If Statement inside the brackets {} of another If Statement, that means the inner If Statement will only be checked if the outer one is true. In your code, think about what happens if answer === 'tails. The 1st if (answer === 'head') will be false, so the code inside its block {} will not run – that means the other If Statement won’t run. Try using 1 If-Else Statement for this puzzle. That way, if the if() part is false then the else {} part will run.
  2. Test Condition

    • The parentheses () of an if() should have code that is either true or false. Usually you want to use ===, >, or <. You have drawBoxes('rrr wrw wrw') inside the parentheses. That code should instead go between the curly brackets {} of an If or Else Statement.

–Frankie


#19

One can also use this code


#20

Is there a bug here? I can’t get past this example. Here is my current code.


#21

Your code is printing out heads, tails instead of a random choice of one or the other. That’s because answer is storing the entire array. When you use pickRandom(answer) on its own line, it creates a string (either ‘heads’ or ‘tails’) but then that string does nothing.

Instead of storing an array in answer try storing pickRandom(['heads', 'tails']). That way, the answer will store either ‘heads’ or ‘tails’.

–Frankie


#22

Hi i have a problem I don’t know what I am doing wrong at this level and I don’t know where to ask help so I Justin do it here here are some screenshots.wel I just found out new users only can’t send one screenshot