If Editor explainer

Aim of the puzzle: Edit an if statement.
Walk through of solution: If statements are used to run a block of code only if a certain condition is true. It looks like this:

if (x === 5) {
  print(x)
}

Inside the () is the test. It uses the equality operator === to check if x is equal to 5. If this is true, then the code inside the {} will run, and x will be printed to the console. If x is not 5, then the program will skip over the if statement, and the code inside the {} will not run.

In this puzzle, the color variable uses pickRandom to choose between the colors blue and green. The variable is then printed to the console, so we can see which color was picked.

The if statement checks if color is equal to blue. If this is true, then a blue box should be drawn.

Sample code solution:

var color = pickRandom([blue, green]);
print(color);

if (color === blue) {
    drawBox(color);
}

Javascript Concepts: If Statements, Variables
Grasshopper Concepts: print(), pickRandom()

12 Likes

why print(color) after the variable declaration is there?
I don’t understand

3 Likes

I got it now.
I thought this code was not running

1 Like

umm just lost with this one Im not sure WHAT im not getting stuck in limbo land

1 Like

Hey there, I can answer any questions you have, or take a look at your code if you post a screenshot.

6 Likes

Should’nt the colors blue and green in the pickRandom be in a string?

var color = pickRandom([‘blue’,‘green’])

and
if(color ==== ‘blue’)

Yes, they should! However, in the very beginning of our curriculum (early Fundamentals I), before we have taught strings, we use variables like blue and green that store the strings 'blue' and 'green'.

In this puzzle, as it’s still very early on, I think it makes sense to use these variables as JavaScript is case sensitive. If we used the string 'blue', a lot of users might instead put 'Blue' and not realize why their if statement isn’t working.

This would distract from the if statement being taught, and the lesson about case-sensitivity can be taught later in the course when users are a bit more comfortable with JavaScript.

Hope this answers your question!
Ben

2 Likes

my brain is in immense pain

3 Likes

Generally, thanks to the Google team, we must learn NLP.

Yes thank … that cleared one of my big doubts . :slight_smile:

1 Like

1 Like

Hey there, it looks like you replaced blue identifier with green identifier inside the if statement. The blue message box will appear here if you replace blue identifier with green identifier.

Hope this helps!
Pummarin

I am giving print(color) statement before the drawbox() statement, but why should the result showing drawbox above the color name

1 Like

I dont know what is wrong with mine

No it’s drawBox(blue) not color

It, is not working it is showing error To complete the puzzle, add drawBox(blue) to the code block of the if statement.