---
# System prepended metadata

title: 小組第四周JS複習

---

# ==討論日期 : 4/3 ( 三 )==

## 上週複習
- [討論日期：3/27](https://hackmd.io/qQicL-fbQBq-wNWBLvCITw?both)
<br>

## 💖 本週複習 🥰

[核心篇第四堂：物件與記憶體位置](https://hackmd.io/@hexschool/HJ0DpbL4i)
<br>

### ==1. 陣列題==
```javascript
// Given an integer array arr and a mapping function fn, return a new array with a transformation applied to each element.

// The returned array should be created such that returnedArray[i] = fn(arr[i], i).

// Please solve it without the built-in Array.map method.

 

Example 1:

Input: arr = [1,2,3], fn = function plusone(n) { return n + 1; }
Output: [2,3,4]
Explanation:
const newArray = map(arr, plusone); // [2,3,4]
The function increases each value in the array by one. 
Example 2:

Input: arr = [1,2,3], fn = function plusI(n, i) { return n + i; }
Output: [1,3,5]
Explanation: The function increases each value by the index it resides in.
Example 3:

Input: arr = [10,20,30], fn = function constant() { return 42; }
Output: [42,42,42]
Explanation: The function always returns 42.
```

#### 相關知識點

#### 大家回答
```javascript
const arr = [1, 2, 3];
const fn = function constant(n, i) {
  return n + i;
};
function map(arr, fn) {
    const newArr = [];
    arr.forEach((item, index) => {
          newArr[index] = fn(item, index);
        });
    return newArr;
}
console.log(map(arr, fn));
```



<br>

### ==2. 陣列題==
```javascript
// 給定一個陣列，裡面有重複的數字。請實踐一個 removeDuplicate 函式，函式會接收一個參數，此參數是一個陣列，最後函式會輸出去除重複數字後的陣列。
const originalArr = [9,2,2,2,5,8,3,3,8,111,9,111]

const ans = removeDuplicate(originalArr);
console.log(ans) // [9,2,5,8,3,111]
```
#### 相關知識點

#### 大家回答

```js=
function removeDuplicate(ary) {
    const result = [];
    originalArr.forEach(function (item) {
        if (!result.includes(item)) {
            result.push(item);
        }
    });
    return result;
}
```

```js=
const originalArr = [9,2,2,2,5,8,3,3,8,111,9,111]

const removeDuplicate = (arySet) => arySet.filter((item, index, ary) => ary.indexOf(item) === index)

const ans = removeDuplicate(originalArr);

console.log(ans) // [9,2,8,3,111]
```

```js=
function removeDuplicate(ary){
  return [...new Set(ary)]
}
```

```js=
const originalArr = [9,2,2,2,5,8,3,3,8,111,9,111]
const ans = [... new Set (originalArr)]
console.log(ans) //(6) [9, 2, 5, 8, 3, 111]
```

```js=
function removeDuplicate(arr) {
  let newArr = [];
  arr.forEach((item) => {
    if (!newArr.includes(item)) {
      newArr.push(item);
    }
  });
  return newArr;
}
```

```js=
function removeDuplicate(arr) {
  return Array.from(new Set(arr));
}
```

```js=
const originalArr = [9, 2, 2, 2, 5, 8, 3, 3, 8, 111, 9, 111];
function removeDuplicate(arr) {
    const newArr = [];
    arr.forEach((item, index) => {
      if (arr.indexOf(item) === index) {
        newArr.push(item);
      }
    });
    return newArr;
}
console.log(removeDuplicate(originalArr));
```

```js=
function removeDuplicate(ary){
  const newAry=[]
  ary.sort((a,b)=>a-b).forEach((item,index)=>{
    if(item!==ary[index+1])newAry.push(item)
  })
  return newAry
}
```

<br>

### ==3. [Codewars](https://www.codewars.com/kata/5ba38ba180824a86850000f7/train/javascript)==

```js=
function solve(arr) {
  return arr.filter((item, index, ary) => ary.lastIndexOf(item) === index);
}
```

```js=
const ary = [3, 4, 4, 3, 6, 3];
const newAry = [];
ary.forEach(function (item, i) {
    if (ary.lastIndexOf(item) === i) {
        newAry.push(item);
    }
});
console.log(newAry);
```

```js=
function solve(arr) {
  arr = [...new Set(arr.reverse())].reverse();
  return arr;
}
```

```js=
const originalArr = [3, 4, 4, 3, 6, 3];
function removeDuplicate(arr) {
    const newArr = [];
    arr.forEach((item, index) => {
      if (arr.lastIndexOf(item) === index) {
        newArr.push(item);
      }
    });
    return newArr;
}
console.log(removeDuplicate(originalArr));
```

<br>


### 問題測驗 (溫馨提醒記得複習前面幾周的唷!)

#### ==題目一==
```js=
var person = {
  name: '小明',
  age: 32,
  1: '2',
  gender: 'male',
  interests: ['吃飯', '睡覺', '打動動'],
  greeting: function() {
    console.log("Hi! I'm " + this.name[0] + '.');
  },
  哈囉: function() {
    console.log('我是' + this.name);
  }
};
```

問：取得「哈囉」
1. `person.哈囉`
2. `person['哈囉']`
<br>

問：取得 1
1. `person['1']`
2. `person[1]`
<br>

#### ==題目二==
```js=
var a = 1;
a.a = 2;
console.log(a.a);
```
1. 1
2. 2
3. undefined

解答：3 (原始型別不能加屬性)
<br>

#### ==題目三==
```js=
var a = new Number(1);
a.a = 2;
console.log(a.a)
```
1. 1
2. 2
3. undefined

解答：2 (物件可以加屬性)
<br>

#### ==題目四==
```js=
var a = 1;
function fn() {
  a = fn.a;
}
fn.a = 2;
console.log(a);
```
1. 1
2. 2
3. undefined

解答：1
```js=
var a = 1;
function fn() { //函式沒執行
  a = fn.a;
}
fn.a = 2;// 函式的屬性
console.log(a);//只看變數
```
<br>

#### ==題目五==
```js=
var a = {};
var b = { b: 'c' };
var c = { c: 'b' };
a[b.b] = 123;
a[c.c] = 456;
console.log(a['b']);
```
1. 123
2. 456
3. undefined

解答：2

解釋 1：

```javascript=
var a = {};
var b = { b: 'c' };
var c = { c: 'b' };
a[b.b] = 123; // 不用理他
a[c.c] = 456; // c.c 是 'b'，視為 a['b']
console.log(a['b']);
```

解釋 2：
```
a[b.b] = a['c'] = 123
a[c.c] = a['b'] = 456
```

<br>

#### ==題目六==
```js=
var prop = '1';
var obj = {
  1: function() {
    console.log('1');
  },
  prop: function() {
    console.log('2');
  }
};
obj[prop]();
```
1. 1
2. 2
3. undefined

解答：1


<br>

#### ==題目七==
```js=
var a = {};
a.a = a;
console.log(a.a.a === a.a);
```

解答：true
<br>


