# 🏅 5/6 (四) 每日任務 Day 9 ###### tags: `Vue 直播班 - 2021 夏季班` 請同學先閱讀完 「[JavaScript 表達式觀念及運用 - JS Expression](https://wcc723.github.io/development/2020/09/17/js-expression/)」 這篇文章後再作答。 題目 (直接貼上答案) --- #### 問題一 (判斷何者為表達式): ```js= if(Ans) { console.log("Ans 是表達式"); } ``` 請問以下有哪些選項代入 Ans 可以輸出 console.log 不會報錯? 補充: [真值假值觀念](https://developer.mozilla.org/zh-CN/docs/Glossary/Truthy) ``` 1. 2 === 2 2. "string" 3. var a 4. if(true){let a = 2;} 5. [25, 30] // 純陣列 6. {name: "hexSchool"} // 純物件 ``` #### 問題二(表達式的應用): ```js= function hexIsGood() { return true; } ``` 以上程式碼為一個「表達式」,請大家到 [MDN](https://developer.mozilla.org/en-US/) 或是透過 Google 搜尋,尋找有哪些程式運作可以插入以上的表達式,仍能正確運行。 舉例 (if 判斷式): ```js= function hexIsGood() { return true; } if(hexIsGood()) {console.log("此程式碼可以運作")}; ``` 回報流程 --- 請同學依照下圖教學觀看解答、回報答案: ![](https://i.imgur.com/QtL8zEW.png) 回報格式如下圖,請在「回報區」使用註解回報答案 (為了統計人數,請同學依序加上「報數」) ![](https://i.imgur.com/L7kyew8.png) <!-- 解答一 1, 2, 5, 6 因為它們都是表達式,會回傳結果。 --> <!-- 解答二 // Ex1: 可以帶入三元運算子 function hexIsGood() { return true; } console.log(hexIsGood() ? '很棒棒' : '不棒棒'); // Ex2: 可以代入樣板字面值 function hexIsGood() { return true; } let str = `六角學院很讚: ${ hexIsGood() }`; console.log(str); --> 回報區 --- <!--1:Alpha 問題一:1,2,5,6 問題二: function hexIsGood() { return true; } switch(hexIsGood()) { case(true): console.log('此程式碼可以運作'); break; } --> <!-- 2: Sean Liu codepen: https://codepen.io/LuckyTiger/pen/jOBOmeM?editors=0011 --> <!--3:Jasmin 問題一:1,2,5,6 問題二: function hexIsGood() { return true; } let getTrue = `我拿到值了 ${hexIsGood()}`; console.log(getTrue); --> <!--4:Eric-小偉哥 問題1-1: if(Ans){} 屬於陳述句 問題1-2: 1,2,5,6 問題二: hexIsGood? "awesome": "soso..."; --> <!--5:Li Shang 問題一:1,2,5,6 問題二: //1.console.log的值必須要是表達式 console.log(hexIsGood()); //2.三元運算子 hexIsGood()?"城市可以運作":"城市也可以運作拉";; --> <!-- 6: Vic 問題一 : 1、2、5、6 問題二 : console.log(typeof hexIsGood()) --> <!-- 7: 小魚 問題一 : 1、2、5、6 問題二 : switch(hexIsGood()) { case(true): console.log('hexIsGood(),此程式碼可以運作'); break; } --> <!-- 8: chou allen codepen: https://codepen.io/eepson123tw/pen/gOmORzw?editors=1111 --> <!-- 9: Alysa Chan // 問題一 // 1,2,5,6 // 3 用 var 宣告變數,是陳述式 // 4 用 if 做流程控制,是陳述式 // 問題二 https://codepen.io/alysachan/pen/MWpWoEG?editors=0011 --> <!-- 10: harold 問題一答案:1,2,5,6(表達式) 問題二答案: // Ex1: 可以帶入while 陳述式 function hexIsGood() { return true; } let i = 0; while (hexIsGood()) { console.log('我的心中每天開出一朵小花'); if (i === 3) { break; } i++; } --> <!-- 11:Jack 問題一 答案:1,2,5,6(表達式) 問題二 答案: function hexIsGood() { return true; } console.log(hexIsGood()? true:false) --> <!-- 12:Iven 問題一 答案:1,2,5,6 為表達式 問題二 答案: function hexIsGood() { return true; } console.log(hexIsGood()?'函式hexIsGood回傳結果true,所以得到這個值':'函式hexIsGood回傳結果false'); --> <!-- 13:yijun 問題一:1,2,5,6 問題二: function hexIsGood() { return true; } 1.帶入console.log() console.log(hexIsGood()); 2.帶入三元運算子 console.log(hexIsGood() ? "good" : "soso"); --> <!-- 14: Lina Chen 問題一: 1,2,5,6 問題二: 三元運算子 function hexIsGood() { return true; } console.log(hexIsGood()? 'work!': 'not work'); // work! --> <!--15: Yi Chieh 問題一 答案:1、2、5、6 問題二 答案: function hexIsGood() { return true; } switch (hexIsGood()) { case true: console.log('這是 true'); break; case false: console.log('這是 false'); break; default: console.log('不是 true/false'); } --> <!-- 16: youting codepen: https://codepen.io/youtingluo/pen/ZEeEJoz?editors=0012 --> <!--17: Ethan 問題一 答案:1、2、5、6 問題二 答案: function hexIsGood() { return true; } function hexIsReallyGood(ans) { return ans; } console.log(hexIsReallyGood(hexIsGood())); --> <!-- 18: wei 問題一 答案:1,2,5,6 問題二 答案: switch(hexIsGood()){ case(true):console.log("此程式碼可以運作") break; } --> <!-- 19:Dah 問題一 (判斷何者為表達式): 1、2、5、6 問題二 const objfn={name: hexIsGood()} console.log(`${objfn.name}`) --> <!-- 20: Sec codepen: https://codepen.io/Sentiments/pen/wvJvroL?editors=0010 --> <!-- 21:陳sam 問題一 答案:1,2,5,6 問題二 答案: function hexIsGood() { return true; } var str = `<li>${hexIsGood(str)}</li>` hexIsGood(str) console.log(hexIsGood(str)?'可以執行':'無法耶') --> <!-- 22: Izumi 泉 codepen:  https://codepen.io/izumi-dev/pen/gOgEZzM --> <!-- 23: Larry codepen:  https://codepen.io/manpower0708/pen/mdWdBMa?editors=1011 --> <!-- 24: Joe Kuo 問題一解答:1,2,5,6 問題二解答: function hexIsGood() { return true; } 將具名 / 不具名函式以指派給變數的方式宣告函式: let str = `頭好痛 is ${ hexIsGood() }`; console.log(str) --> <!-- 25: SONYKO 問題一 解答: 1 2 5 6 問題二 解答:  function hexIsGood() { return true; } let n = 0 while(hexIsGood()){ n++ } --> <!-- 26: Erica codepen: https://codepen.io/ericadu/pen/BaWamWL --> <!-- 27: Chiang 問題一 解答: 1 2 5 6 問題二 解答:  function hexIsGood() { return true; } console.log(hexIsGood() ? "GOOD" : "BAD"); //GOOD --> <!-- 28: 阿倫 問題一 解答: 1,2,5,6 問題二 解答: function hexIsGood() { return true; } console.log(hexIsGood?'好棒':'好棒棒'); --> <!-- 29:Ted Kuo 問題一: 1, 2, 5, 6 問題二: console.log(hexIsGood() === hexIsGood()) --> <!-- peter.chen1024 問題一 解答: 1,2,5,6 問題二 解答:console.log(hexIsGood()?"這是表達式":"這不是表達式") --> <!-- 31: Amanda Chiang 問題一: 1, 2, 5, 6 問題二: console.log(hexIsGood() ? "Lovely!" : "Pity~"); --> <!-- 報數 32: Josh Fang 問題一: 1, 2, 5, 6 問題二: console.log(hexIsGood() ? 'yes' : 'no'); --> <!-- 報數 33: JessieCheng 問題一: 1, 2, 5, 6 問題二: function hexIsGood () { return true; } let array = ['j', 's', 'v', 'u', 'e']; console.log(array.filter(item => hexIsGood())); // [ 'j', 's', 'v', 'u', 'e' ] --> <!-- 報數 34: Wendy Li CodePen: https://codepen.io/rockayumitw/pen/RwpweoY?editors=0011 --> <!-- 報數 35: jimmyFang 問題一: 1 , 2, 5, 6 問題二: // 解答一: function hexIsGood() { return true; } if(hexIsGood()) {console.log(hexIsGood() ? '減肥' : '不減肥')}; --> <!-- 報數 36: Tori 問題一 (判斷何者為表達式): 1. 2 === 2 // 表達式 // O 2. "string" // 表達式 // O 3. var a // 陳述式 // X 4. if(true){let a = 2;} // 陳述式 // X 5. [25, 30] // 表達式 // O 6. {name: "hexSchool"} // 表達式 // O 問題二(表達式的應用): 直接印出 console.log(hexIsGood()); if判斷 if(hexIsGood()){ console.log('是對的'); } 三次元判斷式 console.log( hexIsGood() ? '對的' : '錯的' ) 樣板字面值 let hot = `最近很熱嗎: ${ hexIsGood() }`; console.log(hot); --> <!-- 報數 37: RitaHuang 問題一. 項目 1, 2, 5, 6 會回傳結果,因為皆為表達式。 問題二. function hexIsGood() { return ture; } console.log(hexIsGood()? '可以完課' : '不可以完課'); --> <!--報數38: Fred Chang 問題一: 1.2.5.6 問題二: function hexIsGood() { return good; } console.log(hexIsGood() ? 'good' : 'bad'); --> <!-- 報數 39: leolee (1)答案:1,2,5,6(任何可回傳結果都可稱為表達式); (2)答案: if(hexIsGood()) { console.log("此程式碼可以運作") }; let pika = [1,2,3] function pikachu() { pika.filter(function(item){ return console.log(hexIsGood()); }) } pikachu() console.log會回傳true,ture,ture codepen: https://codepen.io/nekorice/pen/JjWjedm --> <!-- 報數 40::YOYO 問題一 (判斷何者為表達式): 1,2,5,6 問題二(表達式的應用): function hexIsGood() { return true; } // for 迴圈 for (let i=hexIsGood(); i < 10; i++) { console.log(i + 'foorloop succeess') } --> <!-- 報數 41::Jrhung-Tsai 問題一 (判斷何者為表達式): 1,2,5,6 問題二(表達式的應用): function hexIsGood() { return true; } try { //throw [expression] throw hexIsGood(); } catch (err) { if(err)console.log('六角好棒棒'); } //output: 六角好棒棒 --> <!-- 報數 42: Alicia Lo 問題一 (判斷何者為表達式): 答案為1 2 5 6。因為if陳述式內的condition條件必須為表達式,1256都能回傳值所以屬於表達式,因此1256代入不會報錯。 問題二(表達式的應用): // 1.switch switch (hexIsGood()) { case true: console.log("ture") break; default: console.log("default") } // 2.樣板字面值 console.log(`${hexIsGood()}`); --> <!-- 報數 43 : Valerie 第一題答: 1, 2, 5, 6 第二題答: let data = [1,2,3]; data.map((item) => {console.log(hexIsGood())}); --> <!-- 報數 43 : Echo Hui 問題一: 1, 2, 5, 6 問題二: 可以在console.log 正常運行 function hexIsGood() { return true; } console.log(hexIsGood() ? 'good' : 'bad'); --> <!-- 報數 44 : Poseidon 問題一: 1, 2, 5, 6 問題二: 可以在console.log 正常運行 function hexIsGood() { return true; } let str = `六角學院很讚: ${ hexIsGood() }`; console.log(str); --> <!-- 報數 45 : Ed Huang 問題一: 1,2,5,6 問題二: 可以在console.log 正常運行 function hexIsGood() { return true; } console.log(hexIsGood() ? 'yes' : 'no'); --> <!-- 報數 46 : Stacey Huang 問題一: 1,2,5,6 問題二: 可以在console.log 正常運行 function hexIsGood() { return true; } console.log( hexIsGood()? 'Vue.js好棒棒': 'Vue.js好難') --> <!-- 報數 47 : 涂阿銘 問題一:請問以下有哪些選項代入 Ans 可以輸出 console.log 不會報錯? 1. 2 === 2 //可 2. "string" //可 3. var a //錯誤 4. if(true){let a = 2;} //錯誤 5. [25, 30] // 純陣列 //可 6. {name: "hexSchool"} // 純物件 //可 問題二:(表達式的應用): function hexIsGood() { return true; } console.log(hexIsGood ? 'True' : 'False'); --> <!-- 報數 48: shoppingq CodePen: https://codepen.io/shoppingq/pen/eYvmoQw --> <!-- 報數 49: Tofu Tseng 問題一: Ans 是表達式,if(...){...} 是陳述式 問題一之二:1256 2 === 2 // 表達式 "string" // 表達式 var a // 陳述式 if(true){let a = 2;} // 陳述式,會噴錯誤,Unexpected token 'if' [25, 30] // 表達式 {name: "hexSchool"} // 表達式 問題二: 第一種 - 三元運算子 hexIsGood() ? console.log("六角好棒!") : console.log("永遠都不會執行這裡啦"); 第二種 - for 迴圈搭配 if for (let i = 0; i < 5; i++) { if (hexIsGood()) { console.log(`這裡永遠會執行,執行第${i}次!`); } } 第三種 - console.log(Number(hexIsGood())); --> <!-- 報數 50: 圈圈 問題一 : 1、2、5、6 問題二: //三元運算子 hexIsGood ? console.log("此程式碼可以運作") : console.log('no') --> <!-- 報數 51: Jay A1: 1, 2, 5, 6 A2: Expression 定義爲:任何能夠被解析爲一個值的式子皆爲 Expression 分別為兩種類型,一種是本身有值,例如:已宣告的變數(不論存的是什麼) 另一種則爲賦值運算,例如:邏輯運算、數學運算 我們可以歸納為數值本身及只包含運算子的式子皆必爲 Expression 也可以說 Expression 相當於一個命題,可以判斷真假 判斷真假則可以藉由 Boolean() 來操作 這裡有個比較特別的例子就是宣告 function 也是 Expression 因爲 function 是一個物件,所以被當成參數丟進 Boolean()後 根據 ToBoolean 的規則,物件一定會回傳 true 可以改成想像 function 既然可以被賦值給變數,所以 function 也是值 同時根本的概念是 function 為一個物件 --> <!-- 報數 52: Yiren 問題一: 1,2,5,6 問題二: console.log(hexIsGood() ? 'Good' : 'Bad'); --> <!-- 報數 53: moitw 表達式: 1,2,5,6 可替代: console.log(hexIsGood() ? '此程式碼可以運作' : '無法運作'); --> <!-- 報數 54: Carol 表達式: 1,2,5,6 可替代: console.log(hexIsGood()?"此程式碼可以運作":"此程式碼不可以運作"); --> <!-- 報數 55: Amber 問題一: 1,2,5,6 問題二: function hexIsGood() { return true; } console.log(hexIsGood()? 'Good': 'Not good'); // work! --> <!-- 報數 56: Andrew Chi 問題一 : 1、2、5、6 問題二 : console.log(typeof hexIsGood()); --> <!-- 報數 57: Emily Hsi 問題一 : 1, 2, 5, 6 問題二 : function hexIsGood() { return true; } console.log(hexIsGood() ? '棒' : '超級棒'); --> <!-- 報數 58: MM 問題一 1、2、5、6 問題二 console: function hexIsGood() { return true; } console.log(hexIsGood()) 代入樣板字面值: function hexIsGood() { return true; } let str = ` ${hexIsGood()} 函數 `; console.log(str); --> <!-- 報數 59: Butters 問題1 不會報錯:1, 2, 5, 6 問題二 hexIsGood()=== true? console.log('true'): console.log('false'); console.log(`${hexIsGood()},可以運作`) <!-- 報數 60: WA codepen: https://codepen.io/ldddl/pen/BaRBewZ?editors=0010 -->