第三週小組任務 本周討論主題為迴圈應用方式及判斷式。 我們是上次跟第二週小組討論一起開 ZM 討論, 經過討論決定做 `for`、`while` 、`switch` ![](https://i.imgur.com/Ik38Fme.png) ![](https://i.imgur.com/1S6Vcu2.png) ## for ### example #### Ethan Chang ``` //for 迴圈練習範例 let farms = [ { Origin: '台北', Guava: 200, pineapple: 5000 }, { Origin: '台中', Guava: 50, pineapple: 1000 }, { Origin: '高雄', Guava: 120, pineapple: 3215 } ] // 計算今年的鳳梨採收總數 let farmsTotal = farms.length; let thisYearPineappleGoal = 0; //鳳梨總數是 0 , 因為還不知道農田所有的鳳梨數量是多少,而我們預期會得到一組數字所以先宣告一個為 0 的初始值。 for (let i = 0; i < farmsTotal; i++) { thisYearPineappleGoal += farms[i].pineapple; } console.log('今年的鳳梨總採收量是:' + thisYearPineappleGoal); ``` #### AlmeeWong ``` let sales = [ { Seller: '小美', water: 10, candy: 100, paper: 30 }, { Seller: '小明', water: 50, candy: 150, paper: 10 }, { Seller: '花花', water: 80, candy: 30, paper: 195 } ] // 計算今年的水總賣出數量 let salesTotal = sales.length; let thisYearWaterGoal = 0; //水的賣出量總數是 0 , 因為還不知道商家所有的水數量是多少,而我們預期會得到一組數字所以先宣告一個為 0 的初始值。 for (let i = 0; i < salesTotal; i++) { thisYearWaterGoal += sales[i].water; } console.log('今年的水總賣出數量是:' + thisYearWaterGoal); ``` #### 阿倫 ``` //輸入中華職棒2020各隊數據 let CPBL= [ { Team:'Lions', HomeRun:143, Hits:1261, Errors:101 }, { Team:'Monkeys', HomeRun:137, Hits:1361, Errors:130 }, { Team:'Brothers', HomeRun:143, Hits:1319, Errors:96 }, { Team:'Guardians', HomeRun:138, Hits:1227, Errors:119 }, ] //計算今年的總全壘打數 let CPBLTotal= CPBL.length; let CPBL2020HomerunTotal = 0; //因為還未知全壘打數為何,因此設初始值為0 for (let i = 0; i < CPBLTotal; i++) { CPBL2020HomerunTotal += CPBL[i].HomeRun; } console.log('今年的全壘打總數為:' + CPBL2020HomerunTotal); ``` ## while ### example #### Lary ```js //只要條件為true,就會一直執行循環 //題目:南投雙龍瀑布吊橋最多同時限載250人 let waterfallTourists = 0; while (waterfallTourists <= 250) { waterfallTourists += 1; console.log(`此時吊橋上有${waterfallTourists}人`); }; console.log(`吊橋人數已滿,請稍等`); ``` ## switch ### example #### 涂阿銘 ``` 假設 1~3 月春天,4~6 月夏天,7-9 月秋天,10-12 月冬天 let month = 12; switch ( month ){ case 1: case 2: case 3: console.log('春天'); break; case 4: case 5: case 6: console.log('夏天'); break; case 7: case 8: case 9: console.log('秋天'); break; case 10: case 11: case 12: console.log('冬天'); break; default: console.log('月份錯誤'); } ``` #### jason06286 ``` let light='red'; //填入要比對的條件 switch (light) { case 'green': console.log('現在是綠燈!!') break; case 'yellow': console.log('現在是黃燈!!') break; case 'red': console.log('現在是紅燈!!') break; default: break; } ``` ## 參與討論的組員名單 jason06286 AlmeeWong 阿倫 涂阿銘 Lary Ethan Chang