Flip a Coin explainer


#1

The aim of this puzzle: Draw a different picture depending on the result of a coin flip.

Walkthrough of the solution: The answer variable stores a string picked at random from the array. It will either hold ‘heads’ or ‘tails’. The value gets printed out and then it’s time to draw the boxes. But first, we need to know which string the answer is storing, so we use an if statement and check if the answer is equal to ‘heads’. If it is, then we draw a picture. Now we need to add another check for when answer is ‘tails’, so after the first if statement is over, we add another if statement that checks if answer is equal to ‘tails’. In the block of that new if statement we add a drawBoxes() call and a string that describes a pattern of colors. Try giving it a different pattern than the one for ‘heads’.

Sample code solution:
(Tap below to reveal)

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

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


#2

This is great, but after quite some time, I simply am unable to include answer === AND the string; it’s only allowing me to do one or the other. Suggestions or maybe just an error?


#3

Closed and opened the app. It’s fixed. I’ll leave this up in case someone else has the same experience.


#4

look,

we set up var

we print var

AFTER that are IF statements.

That do not seem logical - that you can issue Print before checking with if statements, and on-screen to be shown Drawboxes first as opposed to the in-line code - structured as print first.

wouldn’t it make more sense to struct code to set var, check if statements and draw, and THEN print , as witnessed on-screen?

like this, it implies you can throw if check+drawboxes anywhere within the code and drawboxes will be printed first?

Really confusing


#5

That’s correct. The screen is split into two parts: images on the top, text on the bottom. This imitates the console which will be taught in future lessons. The console is a separate section for text to be printed out. It’s often used like captions for what’s happening in your code; you watch the output on the screen and the console gives little behind-the-scenes notes.

–Frankie


#6

Why can’t the code separate


#7

Could you explain what you mean by the code separating or being separate?

–Frankie


#8

Never mind I got it sir but thank you


#9

Hi guys! I don’t understand what I did wrong! It doesn’t give me to go further :frowning: , but I would like to


#10

It looks like you just need to change your 1st If Statement test from 'head' to 'heads' (plural).

–Frankie


#11

What does === mean? Please explain


#12

I explained the difference between = and === here.

–Frankie