Flip a Coin explainer

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()

5 Likes

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 Likes

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

6 Likes

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

1 Like

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

Why can’t the code separate

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

–Frankie

1 Like

Never mind I got it sir but thank you

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

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

–Frankie

What does === mean? Please explain

1 Like

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

–Frankie

warum geht das nicht bitte um hilfe

When checking if 2 strings are equal, the capitalization matters. Try changing 'Tails' to 'tails'.

'T''t'

–Frankie

I totally understand capilization not the drawboxs??

Can’t type anything, tried to add the “tail” input but the code just grays out…so frustrating,both in chrome and explorer the code just doesn’t allow anything…I guess, that’s my stop, Im out.

1 Like

Tried that, doesn’t work. Any other suggestions?

How do i know which color is assigned to which alphabet? do they have fixed allocation in the database?

Is there a reason the original example is not loading? I have answered the question correctly and it still won’t move on…

Hey there, this is an issue with the code that powers the Your Solution and Example Solution not downloading properly. This typically happens on slower connections, or if an internet connection is interrupted. We’re working on a fix, but for now, try refreshing the page and trying again. Other users have reported that the issue goes away after a few tries, or when the connection improves.

Hope this helps!
Ben