---
# System prepended metadata

title: "\U0001F3C5 Day 18 - 函式觀念三"
tags: [JS 直播班 - 2023 秋季班]

---

---
tags: JS 直播班 - 2023 秋季班
---
# 🏅 Day 18 - 函式觀念三

依照下方程式碼練習，並回答問題。

### 函式參數觀念 

[課程影音連結](https://courses.hexschool.com/courses/2020111311/lectures/48764712)（函式參數介紹、參數寫法）

1. 函式可以帶入自定義的參數（在此為 num1, num2），並在呼叫函式的時候傳入。
```javascript=
function callNum(num1, num2){
  let total = num1 + num2;
  console.log(total);
}
// num1 等於 1, num2 等於 2
callNum(1, 2);
// num1 等於 2, num2 等於 4
callNum(2, 4);
```

2. 參數只存活在大括號。

```javascript=
function callNum(num){
  // 在此可以成功呼叫 num
  console.log(num);
}
callNum(1);
// 函數外部不可以呼叫 num，這行會報錯 
// UDrncaught ReferenceError: num is not defined
console.log(num);
```


問題
---
1. 觀看以下程式碼，最後會印出什麼，以及變數 `sum` 最後的值
```javascript=
let sum = 1;
function calcNum(x) {
  sum += x;
  console.log(x);
}
calcNum(3);
```
2. 下方程式碼宣告了一個陣列（陣列內是[費波那契數列](https://zh.wikipedia.org/zh-tw/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0)），請完成下方程式碼（補上 `...` 的部分），讓 `calcNextNum()` 函式可以正常執行，並讓陣列中最後兩個數字相加後 push 到陣列中，最後 `fibonacciArr` 陣列值為 `[1, 1, 2, 3, 5, 8]`
```javascript=
let fibonacciArr = [1, 1, 2, 3, 5];
let fibonacciArrLength = fibonacciArr.length;
function calcNextNum(...) {
  ...
}
calcNextNum(fibonacciArr[fibonacciArrLength-1], fibonacciArr[fibonacciArrLength-2]);
console.log(fibonacciArr);
```

## 回報流程

將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔! 
解答位置請參考下圖（需打開程式碼的部分觀看）

![](https://i.imgur.com/vftL5i0.png)

<!-- 解答：  

1. 3, 
   sum: 4

2. 
```javascript=
let fibonacciArr = [1, 1, 2, 3, 5];
let fibonacciArrLength = fibonacciArr.length;
function calcNextNum(last, penultimate) {   // 參數命名可以隨意自訂
  fibonacciArr.push(last+penultimate);
}
calcNextNum(fibonacciArr[fibonacciArrLength-1], fibonacciArr[fibonacciArrLength-2]);
console.log(fibonacciArr);
```
-->




回報區
---


|       Discord        |                                  CodePen / 答案                                  |
|:--------------------:|:--------------------------------------------------------------------------------:|
|Moreene|[CodePen](https://codepen.io/Moreene/pen/KKJMmrQ)|
|kuanju27|[CodePen](https://codepen.io/Eero-Chiao/pen/wvNWdLq)|
|詹姆士|[CodePen](https://codepen.io/z111048/pen/QWYEgWm)|
| 銀光菇 | [CodePen](https://codepen.io/genesynthesis/pen/ExryXaz?editors=0012) |
| Ching | [CodePen](https://codepen.io/tzuchingg/pen/NWorgxy?editors=0011) |
| MDFK | [CodePen](https://codepen.io/maywang/pen/YzBwMeg) |
|Fabio20|[Codepen](https://codepen.io/fabio7621/pen/OJdXgMj)|
| pinyi_9 | [CodePen](https://codepen.io/Wpypy/pen/LYqZLpV?editors=0011) |
| Ann Chou | [CodePen](https://codepen.io/annchou_illu/pen/zYeBzKV) |
| Peng | [CodePen](https://codepen.io/H-Peng/pen/PoVzjmJ?editors=0010) |
| 維哲 | [CodePen](https://codepen.io/kwz1202/pen/BaMzZQm) |
|yuling|[CodePen](https://codepen.io/igzdflpu/pen/abXZwpb)|
|風羽|[CodePen](https://codepen.io/Joy-Huang/pen/NWorgXB?editors=1111)|
|yuan!|[CodePen](https://codepen.io/townyuan/pen/mdvEwxB)|
|hsu0921|[CodePen](https://codepen.io/HSUANIN0327/pen/VwgjWdp)|
|阿榮|[Codepen](https://codepen.io/codeitaday/pen/xxMOraV)|
| Eden |[Codepen](https://codepen.io/iseden/pen/poGbwZB)|
| hsiu8767 |[Copen](https://codepen.io/hsiu8767/pen/eYxzENp?editors=0011)|
| cest_jessie | [CodePen](https://codepen.io/itsjessiechen/pen/rNPLwPa) |
| 小夏 | [CodePen](https://codepen.io/michaelhsia/pen/poGbreJ) |
| yuan2781 | [CodePen](https://codepen.io/ismebir/pen/dyaXzWd) |
| Rxzan | [CodePen](https://codepen.io/Tzuru-Chen/pen/XWOKaXK) |
| Lychee KK | [CodePen](https://codepen.io/kakakala/pen/YzBWxaO?editors=1111) |
| Helen | [CodePen](https://codepen.io/milkteamonster/pen/NWorvgQ) |
| Winnie | [CodePen](https://codepen.io/codepen-io-winnie/pen/YzBWxQv?editors=0011) |
| Alyce | [CodePen](https://codepen.io/alycehwy/pen/VwgjzgE?editors=0010) |
| ShangChu | [CodePen](https://codepen.io/shangchu0412/pen/eYxzGdy?editors=0012) |
| ann6212 | [CodePen](https://codepen.io/yqmegupa-the-styleful/pen/XWOKeBx?editors=1012) |
| damon0323 | [CodePen](https://codepen.io/swk9eny2/pen/mdvEBGV?editors=0011) |
| Ivy | [CodePen](https://codepen.io/Irene-Ke/pen/OJdXONj?editors=0010) |
|stone4584|[Codepen](https://codepen.io/Royen0506/pen/yLZJPEr?editors=1111)|
| ming0712 | [CodePen](https://codepen.io/StevenHuang/pen/VwgjrbK?editors=0010) |
| runweiting | [CodePen](https://codepen.io/weiting14/pen/eYxzygZ) |
| damon0323 | [CodePen](https://codepen.io/swk9eny2/pen/mdvEBGV?editors=0011) |
| CPing | [CodePen](https://codepen.io/CPing/pen/zYeBpPZ) |
| Chia Pin | [CodePen](https://codepen.io/joker-cat/pen/MWLeQvO) |
| Dora | [CodePen](https://codepen.io/dorayu/pen/yLZJpPe) |
| hsuanlee1084 | [CodePen](https://codepen.io/hsuanlee1024/pen/oNmLpPy?editors=1010) |
| 精靈 | [CodePen](https://codepen.io/justafairy/pen/ExryQGr) |
| Ataraxia | [CodePen](https://codepen.io/ataraxia8888/pen/ZEwWOQd) |
| Andy | [CodePen](https://codepen.io/andy31342/pen/OJdXxwR) |
| kawa | [CodePen](https://codepen.io/z83xji6/pen/PoVzQVb?editors=1011) |
| 奔跑吧(GTR150)) | [CodePen](https://codepen.io/Wer-Qwe/pen/oNmLqyY?editors=1111) |
| gebyman | [CodePen](https://codepen.io/gebyman/pen/VwgjxzX?editors=0012) |
| SHUO | [CodePen](https://codepen.io/xrzarxkl-the-reactor/pen/JjxKZEm?editors=1010) |
|荷菓かのり| [CodePen](https://codepen.io/oogxwkvw-the-builder/pen/BaMzVrL?editors=0012) |
|保羅| [CodePen](https://codepen.io/paul-1997/pen/OJdXEGW?editors=0011) |
|shiro| [CodePen](https://codepen.io/chenghsienwang/pen/qBgNyBW) |
|jimmy.0706| [CodePen](https://codepen.io/WeiJun-Mao/pen/MWLeBEx?editors=0010) |
| xin | [CodePen](https://codepen.io/Estherrrrrr999/pen/poGbORo) |
| Rogan | [CodePen](https://codepen.io/RoganHsu/pen/YzBWOEv?editors=0011) |
| santu0868 | [CodePen](https://codepen.io/HatsumiSan/pen/WNPxgLv) |
| claire | [CodePen](https://codepen.io/yi-wen-chen/pen/XWOKPjK) |
| MY | [CodePen](https://codepen.io/ahmomoz/pen/yLZJRpP?editors=1111) |
| yu.t_liu | [CodePen](https://codepen.io/YuT200053/pen/RwvRqoo) |
| Uli | [CodePen](https://codepen.io/uli1313/pen/XWOKoyR?editors=0011) |
| xiaokui_71292|[CodePen](https://codepen.io/pen?template=VwgKwme)|
| phyllis |[CodePen](https://codepen.io/Phyllis-Cheng/pen/BaMLyaP)|
| Vivian2857 | [CodePen](https://codepen.io/uojbohed-the-solid/pen/xxMEZgg?editors=1010) |
| tanuki5863 | [CodePen](https://codepen.io/tanuki320/pen/YzBGweL?editors=1011) |
| Yijing | [CodePen](https://codepen.io/Yi-Jing-71080635/pen/gOqwPeQ?editors=0011) |
| 阿賀#3700 | [CodePen](https://codepen.io/sungho/pen/OJdRMyw) |
| RX00 | [CodePen](https://codepen.io/llqzknqv-the-styleful/pen/KKJgzwM?editors=0011) |
| ⭐️小正 | [CodePen](https://codepen.io/colorgolden/pen/WNPGwow) |
| Mattie | [CodePen](https://codepen.io/mattielin/pen/mdvrEPE) |
| nini1202desu | [CodePen](https://codepen.io/tvxq5206/pen/mdvEzoV?editors=0010) |
| 白夜 | [CodePen](https://codepen.io/Wei903/pen/MWLjmaJ?editors=1111) |
| davidtt | [CodePen](https://codepen.io/David-Tsai/pen/xxMEdPJ?editors=0011) |
| noah | [CodePen](https://codepen.io/lch1222/pen/LYqRLVw?editors=0011) |
| 歐陽龍龍#1061 | [CodePen](https://codepen.io/dizzydog-rgb/pen/ExrgvOm) |
| Lainie | [CodePen](https://codepen.io/Lainie88/pen/abXmaKJ?editors=0011) |
| 蛋塔 |[CodePen](https://codepen.io/Hong-Game1/pen/OJdRBVe)|
| sponge |[CodePen](https://codepen.io/biunpujm-the-flexboxer/pen/gOqwQEG?editors=1011)|
| 郭芙蘭#6374 |[CodePen](https://codepen.io/flora_Kuo/pen/vYbXvNJ?editors=0011)|
|  Jerry | [CodePen](https://codepen.io/ildkosxk-the-encoder/pen/qBgaLmR) |
|  Cosmos| [CodePen](https://codepen.io/violet7755/pen/abXBvYg) |
| Tina02 | [CodePen](https://codepen.io/chjitlox-the-bashful/pen/xxMVrQg) |
| Joy-Chiang | [CodePen](https://codepen.io/fwysphbw-the-vuer/pen/qBgqpWO) |
|小廷 | [CodePen](https://codepen.io/uyxkiake-the-selector/pen/qBgqyjG?editors=0011) |
|OSN|[Codepen](https://codepen.io/osncodepen/pen/RwvoBqR?editors=1112)|
|hsuhsusophie|[Codepen](https://codepen.io/hsuhsusophie/pen/ZEwBmMq?editors=0012)|
|Oria|[Codepen](https://codepen.io/kajing/pen/QWYGoxO?editors=0011)|
|xuan|[Codepen](https://codepen.io/xuan0915/pen/KKJawmb?editors=0012)|
|JasonFu|[Codepen](https://codepen.io/jie-Fu/pen/RwvKPOO?editors=0010)|
|Peggy|[Codepen](https://codepen.io/peggylai/pen/qBgROQe?editors=1011)|
|Rochel|[Codepen](https://codepen.io/rochelwang1205/pen/oNmBEJM?editors=1111)|
|yaoling.liang|[Codepen](https://codepen.io/Yao-Ling-L-/pen/rNPjgex?editors=0011)|
|shcopy|[Codepen](https://codepen.io/shcopy/pen/BaMWeXe)|
|_shun0130|[Codepen](https://codepen.io/koba60052ww/pen/WNPRWxM?editors=0011)|
|Daylily|[Codepen](https://codepen.io/daylily/pen/QWYvydq)|
| shawey.1a2b | [Codepen](https://codepen.io/Shawey/pen/abXVWKX) |
| Hank._.z | [Codepen](https://codepen.io/linoiaby-the-sans/pen/dyaZzjj?editors=1111) |
|ChrisSQR|[Codepen](https://codepen.io/ChrisSQR/pen/ExrbwPL)|
|nikkita_3544|[Codepen](https://codepen.io/vanessa7259/pen/ZEwvZBz?editors=0010)|
|Iris|[Codepen](https://codepen.io/iris831206/pen/xxMYqzg?editors=1111)|
|macihuang|[Codepen](https://codepen.io/macy1215/pen/qBgyjxx)|
|sunny_08149|[Codepen](https://codepen.io/vjgpcfpk-the-builder/pen/zYeLyjJ)
|bf_tsai|[Codepen](https://codepen.io/BF-Tsai/pen/PoVdwZY?editors=1111)
|NathanJames|[Codepen](https://codepen.io/ufo060204/pen/KKJxOpV)|
|skypassion5000|[Codepen](https://codepen.io/skypassion5000/pen/vYbVRbR?editors=0011)|
| 辣椒 | [Codepen](https://codepen.io/chilichen118/pen/ExrOmaL) |
