---
# System prepended metadata

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

---

# 🏅 Day 18 - 函式觀念三

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

### 函式參數觀念 

可參考課程影音「函式 function - 函式參數介紹、參數寫法」章節

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 / 答案 |
| --- | ----- | ----- |
| 01 | 泊岸 | [CodePen](https://codepen.io/qoq77416416/pen/zYgwqQr?editors=0011) | 
|02|voke|[CodePen](https://codepen.io/FiSi-the-lessful/pen/vYoxEZO)|
|03|Jainee|[CodePen](https://codepen.io/Jainee0110/pen/WNVZNZg)|
|04|Hailey|[CodePen](https://codepen.io/sxbokfja-the-flexboxer/pen/ExqwEbY?editors=0012)|
|05|ABow_Chen|[CodePen](https://codepen.io/fred39392001/pen/poMWLPx)|
|06|邵|[CodePen](https://codepen.io/ukscrlno-the-typescripter/pen/RwXLMQV?editors=1011)|
|07|陳睿睿|[CodePen](https://codepen.io/uwmrsusb-the-solid/pen/BaXwrPr?editors=0011)|
|08|tim|[CodePen](https://codepen.io/jskrtivy-the-animator/pen/ExqwEOj?editors=0011)|
|09|Ruby|[CodePen](https://codepen.io/xivgaijl-the-selector/pen/JjgrvPb)|
|10|Ariel| [CodePen](https://codepen.io/ariel0510/pen/NWQaMWe?editors=0011) |
| 11 | Dolce_墨 | [CodePen](https://codepen.io/DolceTseng1026/pen/jOgGxvP) | 
| 12 | Lotte | [CodePen](https://codepen.io/hmkdoxuj-the-decoder/pen/VwoMxOE?editors=0011) | 
| 13 | 毛巾 | [CodePen](https://codepen.io/bqdcjboa-the-solid/pen/rNXGdzg) | 
|14|雪粒| [CodePen](https://codepen.io/bzykdfhw-the-bashful/pen/MWNEBaq) |
|15|david1819| [CodePen](https://codepen.io/David-Lin-the-vuer/pen/yLmzRrK) |
|16|.tsz_ying| [CodePen](https://codepen.io/bekgnohe-the-selector/pen/YzmrJKR?editors=0011) |
| 17 | yuyuhunter_0509 | [CodePen](https://codepen.io/KRcube/pen/zYgEmpa?editors=0011) | 
| 18 | ycl325.etheddie | [CodePen](https://codepen.io/wdvikfuz-the-selector/pen/ZEgXmZg) | 
| 19 | brucelee0629| [CodePen](https://codepen.io/xxhdklwi-the-decoder/pen/vYoeQRg?editors=0011) | 
| 20 | jimmy.0706 | [CodePen](https://codepen.io/JimmyMao/pen/mdNBvyg?editors=0012) | 
| 21 | JRz | [CodePen](https://codepen.io/JRZHENG/pen/PoMJVjw?editors=1111) | 
|  22   |   KUN.    | [CodePen](https://codepen.io/barry91205/pen/ExqwrwR) | 
|  23   | Kaya | [CodePen](https://codepen.io/kayaribi/pen/NWQaowR) | 
| 24 | GTR150 | [CodePen](https://codepen.io/Wer-Qwe/pen/VwoMgVo?editors=1111) | 
| 25 | Mos | [CodePen](https://codepen.io/mos25399/pen/RwXLdEg?editors=0011) |
| 26 | 是在哈囉 | [CodePen](https://codepen.io/miyxdhem-the-solid/pen/mdNBgVX?editors=1111) |
| 27 | 嚼勁先生 | [CodePen](https://codepen.io/James520284/pen/XWveGGW) |
| 28 | Mike | [CodePen](https://codepen.io/mike2049/pen/LYwzojX?editors=0011) |
| 29 | Sonia | [CodePen](https://codepen.io/YUJOU/pen/bGXoJMy?editors=0011) |
| 30 | yuyeh_1212 | [CodePen](https://codepen.io/yuyeh1212/pen/NWQaQVW) |
| 31   |   JIA    | [CodePen](https://codepen.io/nzaicwlw-the-styleful/pen/wvVPwdK?editors=0011) | 
|   32  |   Celine    | [CodePen](https://codepen.io/szuning/pen/zYgPvag) | 
|33|mercury2508.| [CodePen](https://codepen.io/Mercury2508/pen/BaXmKVj) |
|34| ab567ab5 | [CodePen](https://codepen.io/WanTzu-Chiang/pen/ZEgaOaV?editors=0012) |
|35|陳小廷| [CodePen](https://codepen.io/ting1124/pen/LYwObMa?editors=0011) | 
|36|hao jing| [CodePen](https://codepen.io/hjxu/pen/QWeOpoW) | 
|37|Rogan| [CodePen](https://codepen.io/RoganHsu/pen/NWQwaKK?editors=1111) | 
|  38   |   610    | [CodePen](https://codepen.io/YI-LING-LIU/pen/YzmEEWX) | 
|  39   |   joylu0902    | [CodePen](https://codepen.io/joylu0902/pen/wvVPmBg) | 
|  40   | yaoling.liang | [CodePen](https://codepen.io/Yao-Ling-L-/pen/gOVGVPz?editors=1111) | 
|41|a1phaki(小k)|[CodePen](https://codepen.io/a1phaki/pen/WNVXaNN)|
|42|Joannehu|[CodePen](https://codepen.io/nelbabkv-the-flexboxer/pen/ZEgaNKe?editors=1011)|
|43|alec1203|[CodePen](https://codepen.io/soloayaya/pen/rNXYEGQ?editors=1012)|
|44|ABOOS| [CodePen](https://codepen.io/guhungyin/pen/BaXJLbv?editors=0012) | 
|45|jerry_66983| [CodePen](https://codepen.io/Barry-C/pen/yLmpgbv?editors=0012) | 
|  46   |  chris     | [CodePen](https://codepen.io/chris-chen-the-selector/pen/rNXpjqP?editors=0010) |
|  47   |  Jack   | [CodePen](https://codepen.io/kxbhixte-the-sasster/pen/mdNppOM?editors=0011) |
|48|Seris| [CodePen](https://codepen.io/Dale-Chien/pen/LYweKGM) |
| 49 | Jim | [CodePen](https://codepen.io/JimChou/pen/eYqyqdo?editors=0011) |
|  50   |   Emma    | [CodePen](https://codepen.io/Emma0919/pen/poMayvz) | 
| 51 | Glen_69515| [Codepen](https://codepen.io/glenyaochih/pen/OJKQvEX)
| 52 | GAVI | [CodePen](https://codepen.io/qbafldcv-the-scripter/pen/jOgzjLq?editors=0011) | 
|53|Tatsu| [CodePen](https://codepen.io/chindesu0207/pen/abeGwWL) | 
|54|Yen| [CodePen](https://codepen.io/yuxxxlouyen/pen/PoMeEvj?editors=0011) | 
|55|Hedwig| [CodePen](https://codepen.io/adirehfz-the-encoder/pen/rNXKBPV?editors=1011) | 
|56|野鴿| [CodePen](https://codepen.io/HoshinoSyouta/pen/ZEgRrLw?editors=0011) |
|57|Ed Huang| [CodePen](https://codepen.io/yide1986/pen/WNVKrvQ) |
|58|蘑菇星星| [CodePen](https://codepen.io/brrrieon-the-vuer/pen/vYoagqV?editors=0011) |
|59|kaka_945| [CodePen](https://codepen.io/kay945/pen/WNVgNvg) |
|60|iamcan| [CodePen](https://codepen.io/uhnxswdq-the-typescripter/pen/poMxaye) |
|61|維妮 | [CodePen](https://codepen.io/Maggy-Lin/pen/QWeJWwO?editors=0011) |
|62|KOMATSU PEI| [CodePen](https://codepen.io/Komatsu2021/pen/eYqQrgm?editors=0012) |
|63|Nocab| [CodePen](https://codepen.io/PeihanWang/pen/QWeYrPY?editors=0012) |
|64|Amy(咂摳)| [CodePen](https://codepen.io/nnxucgmc-the-builder/pen/MWNxvRR?editors=0011) | 
|65|Satar| [CodePen](https://codepen.io/SatarKuo/pen/OPLmJzR?editors=0011) | 
|66|Chuang|[CodePen](https://codepen.io/uidoytjq-the-solid/pen/GgKxwyr?editors=0011)

<!-- 可複製下方格式
|     |       | [CodePen]() | 
-->