Open The Lock explainer


#1

The aim of this puzzle: Update the second if statement so that it’s checking for foundKey being equal to 'no' AND if pinNumber is equal to 10.
Walkthrough of the solution: This puzzle uses the operator && inside the test portion of the if statement — this allows you to test two different variables using just one if statement. The && operator works just like the word ‘and’ (it’s also called the ‘and’ operator!), and requires both tests to be true in order for the code inside the curly brackets {} to run. So, for example:

if (day === 'monday' && vacation !== 'yes') {
  print('You have to go to work today');
}

In the code above it looks to see if the variable day is equal to 'monday' AND checks to see if the variable vacation is not equal to 'yes'. Both the variable day needs to be set to 'monday' AND the variable vacation needs to not be set to 'yes' in order for the sentence ‘You have to go to work today’ to be printed out.

In this puzzle you need to check that the variable foundKey is equal to 'no' AND the variable pinNumber is equal to 10. To do this you just need to update foundKey === 'yes' to be foundKey === 'no' in the second if statement.

Sample code solution:
(Tap below to reveal)

print('pinNumber is ' + pinNumber)
print('foundKey is ' + foundKey)
if (foundKey === 'yes' && pinNumber === 10) {
  print('You opened the lock!');
}
if (foundKey === 'no' && pinNumber === 10) {
  print('You do not have the key')
}

JavaScript Concepts: Logical Operators (&&), Assignments, Calling Functions, Array Expression
Grasshopper Concepts: print()

Additional Code (hidden code that runs before the puzzle’s code):

var pinNumber = 10;
var foundKey = pickRandom(['yes', 'no']);

Unable to complete "Open The Lock"
#3

I’m confused where the foundKey value is coming from in this puzzle. We never set the var.


#4

Hey @TheGratefulDane,

Great question! The foundKey variable is declared in a little bit of hidden code. It’s hidden to allow you to focus on editing the if statement.

The declaration looks like this:

var foundKey = pickRandom(['yes', 'no']);

I hope this helps!
H :slight_smile:


#6

I completely understand, but I kept looking at it, scratching my head. I think hiding this bit makes it more confusing.


#7

Completely agree! We are working on a way to surface this info easily in the app, without distracting from the puzzle. Thanks for the feedback :slight_smile:


#8

Yeah starting at Java and hiding the variable makes it a bit confusing
Knowing what the var is actually helped a lot and i was able to solve it right away


#9

Has the value of pinnumber also been specified as it is taking value 10 always?


#10

Yeah I’ve tried to change it to 9 to check but grasshopper :cricket: said I wasn’t allowed.


#11

This stage is very basic and understandable. Typically I wouldn’t be here, but on my Samsung Galaxy Note 8, after I set up -
if (foundKey === ‘no’)
I am unable to click after ‘no’ and add the && to check for the pinNumber. Everytime I click string and type no, click done and then click the && button, it then adds parenthesis in the statement, which makes sense… but there’s no seeable option to complete this stage for me.
plz fix


#12

Hey @Rex,

Thanks for flagging! This is an issue we’re aware of and working to fix. To add && to an expression, you need to select the expression (which is the hard part). Right now, you can do this by tapping between the === symbol and the variable or value.

Alternatively, you can reset your code and then update foundKey == 'yes' to be foundKey === 'no'.

Thanks for flagging this issue — it helps us to priortize new features.

H :slight_smile:


Unable to complete "Open The Lock"
#13

Yup! The pinNumber variable is defined in hidden additional code like this:

var pinNumber = 10;

H :slight_smile:


#14

Yep, same here I was wondering why we weren’t setting the variable. So confused by this lack of code in the problem. Also the lack of knowing where the strings yes and no come into play. I wish the whole problem was laid out. I think I understand it now though.


#15

Hi, i have a small quiery,why are we using print(‘pin number is’ + pin number) when we can directly write print(pin number)
cant we is it correct or not
And why we add + symbol in front of pin number


#16

Hello, I also came here because I didn’t understand this problem due to code missing. Are there any other levels where code missing so I know to expect it?

It’s more problematic and confusing for beginners if all the code is not present every time.


#17

Hey @AnoopChandran7991,

You can use just print(pinNumber), but that will only print out 10 rather than ‘The pin number is 10’.

The + symbol can be used to add numbers together, as well as combining two strings of letters. You may find this comment on the Math Modifier explainer useful.

H :slight_smile:


#18

Hey @Liquis,

Thanks for the feedback! There are a few other puzzles in the Fundamentals course (and several in the Animations courses) where there is hidden code. As you’re coding ability progresses, in order to focus on new things we often have to hide a bit of the code so that it isn’t overwhelming.

In this case, hiding the variables simply cuts down on the amount of code shown and I agree with you that can be quite confusing. We’re actively working on a way to surface this to users better.

Thanks again for the feedback,
H :slight_smile:


#19

Thank you Heather for the response. Would it be possible to provide a webpage that has a list of all the hidden code for each problem, so we can reference it? While you work on integrating it into the software.

Thanks,
Danny


#20

Hey @Liquis,

For the Fundamentals course, you can see the hidden code in the explainer under the ‘additional code’ section.

Thanks!
H :slight_smile:


#21


Why we have used print in the first two lines ?
@Grasshopper_Heather plz help me to understand it .


#22

The first two print statements are to help you check that your code is running correctly. If you don’t know what the pinNumber and foundKey values are, it’s hard to know if your If Statements have the correct test.

For example, if your code always printed ‘The pin number is right, but you are missing the key.’, why could that be happening? Perhaps the value of foundKey is ‘no’ and the pinNumber is 10 every time you run the code. Or maybe there is a mistake in your If Statement test? It helps to see what the value of foundKey and pinNumber if you want to know which If Statements should be true or false.

–Frankie