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 源碼如下:

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 woo...

Chromecast 一代改造 改善散熱及收訊問題

Google 的 Chromecast 可以放方便的把影像投放到電視上,可是一代的機構設計過於狹小,導致散熱不易,過熱的時候會出現 LAG 或是連線中斷的情況,尤其是使用 Mirror 鏡射功能的時候,或是有連續使用較長的時間,LAG的情況會變得很嚴重。 另外就是收訊問題,一代的天線只有一根,如果放在電視後方(尤其是如果是有鐵殼的電視), 或是基地台距離比較遠導致訊號不好時也會常有 LAG 的情況發生。 Chromecast 二代有對這兩個問題作修正,可是手上已經有Chromecast 一代的怎麼辦? 為了解決一代過熱及收訊不良的問題,對 Chromecast 做些改造改善散熱及收訊部分, 不過只要是拆開了就沒有保固了,不過一代的產品應該也都過保固了就是。 如何拆解請參考 Chromecast Teardown - iFixit https://www.ifixit.com/Teardown/Chromecast+Teardown/16069 拆開之前拍的照片找不到了, 借用 iFixit 圖片方便做說明。 首先把 Chromecast 拆開,周圍看起來好像是用膠或是超音波融合, 需要用些方式從HDMI接頭沿著外圍一一撥開 裡面是由鐵殼做屏蔽,散熱由導熱矽脂將熱量導上殼的一個鋁塊作散熱 圖中上殼內面綠綠的就是導熱矽脂 裡面有一個刻有編號的鋁塊 將鋁塊移除後下方也是使用大量的導熱矽脂做散熱將熱量導到塑膠殼上 散熱不佳一個原因是晶片到屏蔽殼有個不小的高度差(約1mm),屏蔽殼到散熱鋁塊也有個高度差, 散熱鋁塊到外殼也是有高度差,即使是使用了導熱矽脂做熱量的導出, 這些高度差也會導致散熱的速度不佳。 再來即使熱量導到散熱鋁塊,也因為外殼為塑膠材質無法快速的散熱, 最後熱量持續的累積導致溫度過高。 圖: PCBA正面 紅色框框內就是我們主要要加強散熱的晶片位置,也就是要熱量產生最多的來源。 仔細看了一下,手上這版 Chromecast 1 處理器已經改用 Chromecast 2 同樣新版的處理器以及周邊IC ⬤ Marvell ARMADA 1500 Mini Plus SoC (88DE3006) ...