---
# System prepended metadata

title: 測試題

---

# 測試題
**第一題** 創造一個記憶函式
```
let cache = {}
function memorize(x) {
  if (cache[x]) {
    console.log('already exist')
    return cache[x]
  }
  else {
    cache[x] = x * 2
    console.log('add to cache')
    return cache[x]
  }
}
const MA = (A) => {
  memorize(A)
}
```
**第二題** 回傳兩數相加
```
function add (a, b) {
  let arr1 = a.split('').reverse().map(x => parseInt(x))
  let arr2 = b.split('').reverse().map(x => parseInt(x))
  for (let i=0; i<arr1.length; i++) {
    let num = (arr1[i] || 0) + (arr2[i] || 0)
    arr1[i] = num
    let j = i
    while(arr1[j] >= 10) {
      arr1[j] = 10 - arr1[j]
      if (arr1[j + 1]) {
        arr1[j + 1] = arr1[j + 1] + 1
      }
      else {
        arr1[j+1] = 1
      }
      j++ 
    }
  }
  return arr1.reverse().join('')
}
```