---
# System prepended metadata

title: "\U0001F3C5 Day 20 - 函式與陣列物件整合運用"
tags: [JS 直播班 - 2024 秋季班]

---

# 🏅 Day 20 - 函式與陣列物件整合運用

綜合前幾天函式觀念以及陣列物件相關操作，回答下方問題。

問題
---
依照註解敘述，完成下方程式碼（只需要補上 `...` 的部分，其餘不要更動）
```javascript=
let courses = [
  {
    title: 'Javascript',
    members: 400,
    id: 1,
    isStarted: true,
    isSoldout: true,
    tasks: ['主線任務', '每日任務']
  }
];

let newCourse = {
  title: 'Vue3',
  members: 300,
  id: 2,
  isStarted: false,
  isSoldout: true,
  tasks: ['主線任務', '每日任務']
};

// 1. 請將 courses 陣列中 id 為 1 的物件，tasks 陣列新增一筆資料為 '每日刷題'。
// 2. 請完成下方函式，將物件做為參數，判斷當課程中的 isSoldout 屬性為 true 時，
//    就將此物件新增到 courses 陣列中，並回傳 courses 陣列的長度
//   （只需要補上 ... 的部分，其餘不要更動）

function addCourse(courseObj) {
  if(...) {
    courses....;
    return ...;
    console.log('新增成功'); 
  } else {
    console.log('無法新增');
    return ...;
  }
  return;
}
let coursesLength = addCourse(newCourse);

// 3. 完成上方程式碼後，選出正確敘述
// 選項：
// (1) 最後程式碼會印出 '新增成功'，而變數 coursesLength 的值會變為 2
// (2) 最後程式碼不會印出任何字串
// (3) 如果 newCourse 的 isSoldout 屬性值為 false 時，執行 addCourse(newCourse) 將不會印出任何字串，也不會回傳 courses 陣列的長度
```


## 回報流程

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

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

<!-- 解答：  

```javascript=
let courses = [
  {
    title: 'Javascript',
    members: 400,
    id: 1,
    isStarted: true,
    isSoldout: true,
    tasks: ['主線任務', '每日任務']
  }
];
let newCourse = {
  title: 'Vue3',
  members: 300,
  id: 2,
  isStarted: false,
  isSoldout: true,
  tasks: ['主線任務', '每日任務']
};

// 1. 
courses[0].tasks.push('每日刷題');

// 2. 
function addCourse(courseObj) {
  if(courseObj.isSoldout) {
    courses.push(courseObj);
    return courses.length;
    console.log('新增成功'); 
  } else {
    console.log('無法新增');
    return courses.length;
  }
  return;
}
let coursesLength = addCourse(newCourse);

// 3. 完成上方程式碼後，選出正確敘述
// 答案：(2)
```
-->




回報區
---
| #   | Discord | CodePen / 答案 |
| --- | ----- | ----- |
| 01 |泊岸 | [CodePen](https://codepen.io/qoq77416416/pen/vYomKxr?editors=0011) | 
|02|voke|[CodePen](https://codepen.io/FiSi-the-lessful/pen/GRVWgQj)|
|03|Jainee|[CodePen](https://codepen.io/Jainee0110/pen/YzmrzeV)|
|04|hao jing|[CodePen](https://codepen.io/hjxu/pen/VworbeE)|
|05|Sonia|[CodePen](https://codepen.io/YUJOU/pen/eYqeLWm?editors=0011)|
| 06 | yaoling.liang |[CodePen](https://codepen.io/Yao-Ling-L-/pen/KKOyxZj?editors=1010)|
| 07 |a1phaki(小k)|[Codepen](https://codepen.io/a1phaki/pen/OJKOoYq)|
| 08 |tim|[Codepen](https://codepen.io/jskrtivy-the-animator/pen/xxvPyKX?editors=0011)|
| 09 | 毛巾 |[Codepen](https://codepen.io/bqdcjboa-the-solid/pen/Exqbezm)|
| 10 | 倫倫 |[Codepen](https://codepen.io/f5badapple/pen/VworVZo?editors=0012)|
| 11 | Dolce_墨 |[Codepen](https://codepen.io/DolceTseng1026/pen/vYoWVoB)|
| 12 | Ruby | [CodePen](https://codepen.io/xivgaijl-the-selector/pen/NWQwEbg) |
| 13 | Lotte | [CodePen](https://codepen.io/hmkdoxuj-the-decoder/pen/XWvzyEG?editors=0011) |
| 14 | yuyuhunter_0509 | [CodePen](https://codepen.io/KRcube/pen/poMdQLZ?editors=0011) |
| 15 | david1819 | [CodePen](https://codepen.io/David-Lin-the-vuer/pen/mdNqQvW) |
|  16   |  KUN|[CodePen](https://codepen.io/barry91205/pen/bGXYQXM) |
|  17   |    610   | [CodePen](https://codepen.io/YI-LING-LIU/pen/XWvzoQN) |
|  18   | ab567ab5 | [CodePen](https://codepen.io/WanTzu-Chiang/pen/RwXjvgd?editors=0012) |
| 19 |邵| [Codepen](https://codepen.io/ukscrlno-the-typescripter/pen/jOgaJQK?editors=0011)|
| 20 |ycl325.etheddie| [Codepen](https://codepen.io/wdvikfuz-the-selector/pen/vYoWwXQ)|
|    21 |  Celine     | [CodePen](https://codepen.io/szuning/pen/XWvzQam?editors=1010) | 
| 21 | Mos | [CodePen](https://codepen.io/mos25399/pen/VworOjB?editors=0011) |
|  23   |   Ariel •ࡇ•    | [CodePen](https://codepen.io/ariel0510/pen/oNKoRqa?editors=0011) |
| 24    | yuling      | [CodePen](https://codepen.io/yu-ling-peng/pen/MWNrWML?editors=0011) |
| 25 | Kaya | [CodePen](https://codepen.io/kayaribi/pen/qBepBrG) |
|  26  |    JIA   | [CodePen](https://codepen.io/nzaicwlw-the-styleful/pen/eYqyNJB?editors=0011) | 
|  27  |    嚼勁先生   | [CodePen](https://codepen.io/James520284/pen/XWvVKjP) |
|28|睿睿|[CodePen](https://codepen.io/uwmrsusb-the-solid/pen/XWvVKyG?editors=0011)|
|29|mercury2508.| [CodePen](https://codepen.io/Mercury2508/pen/gOVoYxJ) | 
|30|ABOOS| [CodePen](https://codepen.io/guhungyin/pen/abeEmeJ?editors=0012) | 
|  31   |    chris   | [CodePen](https://codepen.io/chris-chen-the-selector/pen/PoMEpwJ?editors=0012) | 
| 32 | GTR150 | [CodePen](https://codepen.io/Wer-Qwe/pen/OJKzpBo?editors=1011) | 
| 33 | jimmy.0706 | [CodePen](https://codepen.io/JimmyMao/pen/GRVygzB?editors=0012) | 
| 34 | JRz | [CodePen](https://codepen.io/JRZHENG/pen/xxvpWBv?editors=1111) |
| 35 | 是在哈囉 | [CodePen](https://codepen.io/miyxdhem-the-solid/pen/gOVojeM) |
| 36 |Hailey|[CodePen](https://codepen.io/sxbokfja-the-flexboxer/pen/jOgYXBe?editors=0010)|
| 37 |Seris|[CodePen](https://codepen.io/Dale-Chien/pen/QWeaXVO)|
| 38 | Jim |[CodePen](https://codepen.io/JimChou/pen/eYqVYLL?editors=0011)|
| 39   |  alec1203     | [CodePen](https://codepen.io/soloayaya/pen/NWQypmE?editors=1112) |
| 40   |  Mike     | [CodePen](https://codepen.io/mike2049/pen/OJKQmae?editors=0012) |
| 41   |  Rogan     | [CodePen](https://codepen.io/RoganHsu/pen/poMaLeP?editors=1111) |
| 42  | Glen_69515 | [CodePen](https://codepen.io/glenyaochih/pen/jOgZxze?editors=1012) | 
| 43  | .tsz_ying | [CodePen](https://codepen.io/bekgnohe-the-selector/pen/dyxdVpZ) |
| 44 | yuyeh_1212 | [CodePen](https://codepen.io/yuyeh1212/pen/RwXQqyw) |
| 45 | ABow_Chen | [CodePen](https://codepen.io/fred39392001/pen/xxvWbKB) |
| 46 |    Emma   | [CodePen](https://codepen.io/Emma0919/pen/rNXdqxB) | 
| 47 |    Joannehu   | [CodePen](https://codepen.io/nelbabkv-the-flexboxer/pen/BaXrYLP?editors=0011) | 
|48|Tatsu| [CodePen](https://codepen.io/chindesu0207/pen/oNKdwyP) | 
|49|Yen| [CodePen](https://codepen.io/yuxxxlouyen/pen/JjgvpGd?editors=0011) |
|50|陳小廷| [CodePen](https://codepen.io/ting1124/pen/NWQMEZN) |
|51|joylu0902| [CodePen](https://codepen.io/joylu0902/pen/dyxeaMX) |
|52|Hedwig| [CodePen](https://codepen.io/adirehfz-the-encoder/pen/bGXKGVZ?editors=1010) | 
|53|野鴿| [CodePen](https://codepen.io/HoshinoSyouta/pen/OJKEvyo?editors=0011) | 
|54|Ed Huang| [CodePen](https://codepen.io/yide1986/pen/jOgpWyM) | 
|55|蘑菇星星| [CodePen](https://codepen.io/brrrieon-the-vuer/pen/wvVxJrK?editors=0012) | 
|56|charlottelee| [CodePen](https://codepen.io/char849/pen/VwoBrgx) | 
|57|kaka_945| [CodePen](https://codepen.io/kay945/pen/poMOobY) | 
|58|維妮| [CodePen](https://codepen.io/Maggy-Lin/pen/LYwXVKK?editors=0010) | 
|59|KOMATSU PEI| [CodePen](https://codepen.io/Komatsu2021/pen/YzmRLOa) |
|60|Nocab| [CodePen](https://codepen.io/PeihanWang/pen/oNKmPgN?editors=0012) |
|61|Amy(咂摳)| [CodePen](https://codepen.io/nnxucgmc-the-builder/pen/qBevePe?editors=0011) | 
|62|Satar| [CodePen](https://codepen.io/SatarKuo/pen/azoWbqX?editors=0011) | 
|63|Chuang|[CodePen](https://codepen.io/uidoytjq-the-solid/pen/GgKxPLw?editors=0011)


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