Skip to main content

Open The Lock - Grasshopper









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

Popular posts from this blog

KSB037 Motor Board 馬達板擴充積木

KSB037 Motor Board 馬達板擴充積木 key: makeblock,microbit,motor Description 說明: 使用 KSB037 Motor Board 馬達板擴充積木 https://github.com/edisonchiu/pxt-motor-KSB037 History v0.1 來源版本 https://github.com/lioujj/KSB037 v0.2 修正字串錯誤 Step 步驟: 1. Makeblock -> Advanced -> Add Package 2. 在 Search or enter project URL… 輸入擴充積木網址,      https://github.com/edisonchiu/pxt-motor-KSB037 3. 按下搜尋會出現擴充積木,按下去新增擴充積木 4. 積木區就會出現新的積木可以用了。 KSB037 馬達擴充板 擴充積木測試程式 KSB037_CustomBlock_Test 目的說明: 按下按鈕可以測試馬達和板子是否正常運作 注意事項: 馬達擴充板 外接馬達需要外接電源,且電源開關需要開啟。 馬達擴充板 指撥開關 2,3,4,5 需要在 ON 的狀態。 功能說明: 按 A 左邊馬達往前全速(1023)運轉。 按 B 右邊馬達往前全速(1023)運轉。 按 A+B 兩邊邊馬達同時往前全速(1023)運轉。 以上皆非則馬達停止顯示中心一個點。 micro:bit - KSB037 Motor Simple Test (JavaScript) Source Code 源碼如下:

Maze - Blockly Games

Blockly Games : Maze 1 Question 問題: Solution 解答: moveForward(); moveForward(); Blockly Games : Maze 2 Question 問題: Solution 解答: moveForward(); turnLeft(); moveForward(); turnRight(); moveForward(); Blockly Games : Maze 3 Question 問題: Solution 解答: while (notDone()) {   moveForward(); } Blockly Games : Maze 4 Question 問題: Solution 解答: while (notDone()) {   moveForward();   turnLeft();   moveForward();   turnRight(); } Blockly Games : Maze 5 Question 問題: Solution 解答: moveForward(); moveForward(); turnLeft(); while (notDone()) {   moveForward(); } Blockly Games : Maze 6 Question 問題: Solution 解答: while (notDone()) {   moveForward();   if (isPathLeft()) {     turnLeft();   } } Blockly Games : Maze 7 Question 問題: Solution 解答: while (notDone()) {   moveForward();   if (is

Image Decoder - Grasshopper

  Image Decoder Details 詳細解說: // 宣告變數 img, 型態自動依初始值設為字串 // 設定初始值為存放影像編碼 var img = "ewvveewvvw wiiwiiw webbwbbew weeggweggw ewyyewyyew woowoowe wrreewrrw"; 程式一開始宣告變數 img 存放影像編碼,影像編碼內穿插了 ‘e’ 這個字元符號,這個多餘的字元符號可以是任何沒有使用到的字元都可以替代,例如使用 ‘x’,或是使用兩種以上都可以,不過程式解碼時要記得都要濾掉。 這些多餘的字元符號把原來的圖案弄亂,所以可以讓不知道解碼的人看不出來是什麼圖案。如果我們收到知道如何解碼,可以把 'e' 字元符號去掉(Filter out 'e’),就可以看到原來的圖案了 IMAGE DATA // 原來要顯示的圖樣 空白字元表示換行 var img =      "wvvwvvw wiiwiiw wbbwbbw wggwggw wyywyyw woowoow wrrwrrw" IMAGE DATA ENCODE // 影像編碼內穿插了 ‘e’ 這個字元符號,把原來的圖案弄亂 var img = "ewvveewvvw wiiwiiw webbwbbew weeggweggw ewyyewyyew woowoowe wrreewrrw" // 解碼部分,依序判斷陣列中的字元不為’e’ 則畫出編碼內容方塊 for (var letter of img) {   if (letter !== ‘e’) {     drawBoxes(letter);   } } // 將變數 img 印出,  print(img); 因為我們並沒有改變 img 內容,所以 img 內容仍是含有 ’e’ 字元未解碼的狀態  YOUR CODE // 宣告變數 img, 型態自動依初始值設為字串 // 設定初始值為存放影像編碼 var img = "ewvveewvvw wiiwiiw webbwbbew weeggweggw ewyyewyyew woowo