---
# System prepended metadata

title: OnlineTest - Javascript 單元 1
tags: [資料庫程式設計, Javascript]

---

######  tags: `資料庫程式設計` `Javascript`

> 有什麼新方法都可以放上來喔
> 我只是放我通過的程式碼而已

# OnlineTest - Javascript 單元 1
##  1. Javascript排序
:::info
將輸入的陣列由大排序到小
EX: [5,8,10,1,3]  =>  [10,8,5,3,1]
```javascript=
function MyMainFunc(input) {
    return input.sort(function(a,b) {
        return b - a;
    });
}
```
:::

##  2. 陣列總和
:::info
將輸入的陣列數值全部加總
EX: [1,2,3]  =>  6
```javascript=
function MyMainFunc(numbers) {
  let sum = 0;
  for (let i=0; i<numbers.length; i++) {
    sum += numbers[i];
  }
  return sum;
}
```
:::

##  3. 取得URL的主機名稱
:::info
將輸入一個URL字串需回傳URL的主機名稱
EX:
- "https://google.com/"   =>   "google.com"
- "http://120.108.204.116/onlinetest" => "120.108.204.116"
```javascript=
function MyMainFunc(url) {
    return url.split("/")[2];
}
```
:::

##  4. 質數判斷
:::info
輸入一個陣列，找出其中的所有質數，回傳時按照輸入的順序排列
EX:
[2,11,5,6,7,8,100,101] => [2,11,5,7,101]
```javascript=
function MyMainFunc(numbers) {
    return numbers.filter((value) => {
        if (value === 1) return;
        for (let i=2; i*i<=value; i++) {
            if (value % i === 0) return ;
        }
        return value;
    });
}
```
:::

##  5. 陣列的最大和最小值
:::info
輸入一個陣列，回傳陣列中最大值+最小值
EX:
- [-100,100,2] => 0
- [1,2,3,4,5] => 6
```javascript=
function MyMainFunc(numbers) {
    return Math.max.apply(null, numbers) + Math.min.apply(null, numbers);
}
```
:::

##  6. 移除重複的單字
:::info
輸入一個字串，單字由空格分開，將重複的單字移除後回傳
EX:
- "abc abc abc" => "abc"
- "aa bb cc aa dd" => "aa bb cc dd"
```javascript=
function MyMainFunc(str) {
    return [... new Set(str.split(" "))].join(" ");
}
```
:::

##  7. 取得timestamp的UTC時間
:::danger
輸入一個數字，為毫秒的時間戳，輸出一個為UTC時間的字串
輸出格式:"YYYY/MM/DD HH:MM:SS"，須補0
EX:
- 1646230477994 => "2022/03/02 14:14:37"
- 0 => "1970/01/01 00:00:00"
```javascript=
function MyMainFunc(timestamp) {
    timestamp -= 8*60*60*1000;
    const d = new Date(timestamp);
    return format(d);
}

function format(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hr = String(date.getHours()).padStart(2, '0');
    const min = String(date.getMinutes()).padStart(2, '0');
    const s = String(date.getSeconds()).padStart(2, '0');
    return year + '/' + month + '/' + day + ' ' + hr + ':' + min + ':' + s;
}
```
> 請教我更好的做法 >~<

:::warning
> 綜合幾個人的 solution

```javascript
function MyMainFunc(timestamp) {
  var date = new Date(timestamp);
  var year = date.getUTCFullYear();  // 直接用 getUTCxxx 取得 UTC 格式時間
  var month = ("0" + (date.getUTCMonth() + 1)).slice(-2); // str.slice(n) 取長度為 n 的子字串
  var day = ("0" + date.getUTCDate()).slice(-2);          // 負代表從後面開始取
  var hr = ("0" + date.getUTCHours()).slice(-2);
  var min = ("0" + date.getUTCMinutes()).slice(-2);
  var sec = ("0" + date.getUTCSeconds()).slice(-2);

  var dateString = `${year}/${month}/${day} ${hr}:${min}:${sec}`;
  return dateString;
}
```
:::


##  8.解析JSON格式
:::info
輸入一個字串，回傳這個json物件key的數量
EX:
- '{"a":1,"b":2}' => 2
- '{"a":1,"b":{"c":2},"c":"1234","d":"e"}' => 4
```javascript=
function MyMainFunc(str) {
    return Object.keys(JSON.parse(str)).length;
}
```
:::

##  9. 展開巢狀陣列
:::info
輸入一個巢狀陣列，將此巢狀陣列展開成一維陣列
EX: [1, 2, [4, [6, 7], 5], [8, 9]] => [1, 2, 4, 6, 7, 5, 8, 9]
```javascript=
function MyMainFunc(array) {
    return array.flat(Infinity);
}
```
:::

##  10. 取代字串
:::info
輸入三個字串，將一個字串(參數1)中的所有指定字串(參數2)取代成新的字串(參數3)
EX:
- MyMainFunc("12321","2","4") => "14341"
- MyMainFunc("abcabc","abc","def") => "defdef"
```javascript=
function MyMainFunc(str, substr, newSubStr) {
    return str.replaceAll(substr, newSubStr);
}
```
:::

