Open The Lock - Grasshopper
Question 問題:
YOUR CODE
var pinNumber = 10;
var foundKey = pickRandom([‘yes’,’no’]);
print(‘pinNumber is ‘ + pinNumber);
print(‘foundKey is ‘ + foundKey);
if (foundKey === ‘yes’ && pinNumber=== 10) {
print(‘You opened the lock!‘);
}
if (foundKey === ‘yes’ && pinNumber=== 10) {
print(‘The pin number is right, but you are missing the key.‘);
}
Sample code solution 解答:
YOUR CODE
var pinNumber = 10;
var foundKey = pickRandom([‘yes’,’no’]);
print(‘pinNumber is ‘ + pinNumber);
print(‘foundKey is ‘ + foundKey);
if (foundKey === ‘yes’ && pinNumber=== 10) {
print(‘You opened the lock!‘);
}
if (foundKey === ‘no’ && pinNumber=== 10) {
print(‘The pin number is right, but you are missing the key.‘);
}
Details 說明:
只要把 foundKey === ‘yes’ 改成 no 就可以
主程式沒把宣告放上去,是為了凸顯主題,不過有可能會造成誤解。
程式前面缺少的宣告變數部分
// 宣告變數 pinNumber 設定初始值為 10,
// 型態自動依初始值設為整數
var pinNumber = 10;
// 宣告變數 foundKey 設定初始值取亂數,
// 型態自動依初始值設為字串 yes 或是 no 其中之一
var foundKey = pickRandom([‘yes’,’no’]);
宣告變數 pinNumber 設定初始值為 10
宣告變數 foundKey 取亂數 yes 或是 no
var pinNumber = 10;
var foundKey = pickRandom([‘yes’,’no’]);
// 將變數 pinNumber 和 foundKey 結果印出,
// 因為 foundKey 有取亂數,可能為 yes 或 no
// 需要多執行幾次就會發現印出的結果有變化
print(‘pinNumber is ‘ + pinNumber);
print(‘foundKey is ‘ + foundKey);
下面兩行就是將執行亂數的結果印出
print(‘pinNumber is ‘ + pinNumber);
print(‘foundKey is ‘ + foundKey);
foundKey會取亂數 要多執行幾次才知道
使用 AND 運算
在 if 中使用 AND 運算。
語法 if ( A AND B )
表示 A 和 B 都必須要為真(true),結果才會為真(true)
AND (Truth Table 真值表)
A
B
RESULT
false
false
false
true
false
false
false
true
false
true
true
true
真值表中的結果我們用 0=false 和 1=true 來替代
A
B
RESULT
0
0
0
1
0
0
0
1
0
1
1
1
所以 AND 運算我們可以記做
0 AND 0 = 0
1 AND 0 = 0
0 AND 1 = 0
1 AND 1 = 1
又 AND 可以用 && 替代
0 && 0 = 0
1 && 0 = 0
0 && 1 = 0
1 && 1 = 1
其他補充資料:
Logic Operator 邏輯運算元(子)
AND &&
OR ||
NOT !
ADD +
Math Operator 算術運算元(子)
SUBB 減法 -
MUL 乘法 *
DIV 除法 /
MOD 模數(取餘數) %
Operators in C and C++ - Wikipedia
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
YOUR CODE
// 宣告變數 pinNumber 設定初始值為 10,
// 型態自動依初始值設為整數
var pinNumber = 10;
// 宣告變數 foundKey 設定初始值取亂數,
// 型態自動依初始值設為字串 yes 或是 no 其中之一
var foundKey = pickRandom([‘yes’,’no’]);
// 將變數 pinNumber 和 foundKey 結果印出,
// 因為 foundKey 有取亂數,可能為 yes 或 no
// 需要多執行幾次就會發現印出的結果有變化
print(‘pinNumber is ‘ + pinNumber);
print(‘foundKey is ‘ + foundKey);
// 判斷如果 foundKey 為 ‘yes’ 且(AND運算) pinNumber 為 10
// 則印出字串 "You opened the lock!"
if (foundKey === ‘yes’ && pinNumber=== 10) {
print(‘You opened the lock!‘);
}
// 判斷如果 foundKey 為 ‘no’ 且(AND運算) pinNumber 為 10
// 則印出字串 "The pin number is right, but you are missing the key."
if (foundKey === ‘no’ && pinNumber=== 10) {
print(‘The pin number is right, but you are missing the key.‘);
}
Open The Lock explainer - Puzzle Explainer - Grasshopper Support
https://support.grasshopper.codes/t/open-the-lock-explainer/159
The aim of this puzzle: Update the second if statement so that it’s checking for foundKey being equal '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.
The foundKey and pinNumber variables are 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']);
var pinNumber = 10;
JavaScript Concepts: Logical Operators (&&), Assignments, Calling Functions, Array Expression
Grasshopper Concepts 5: print()
Additional Code (hidden code that runs before the puzzle’s code):
var pinNumber = 10;
var foundKey = pickRandom(['yes', 'no']);
Comments
Post a Comment