每日進度1 (JS tips) Flashcards

1
Q

for loop in js

A
  • for…of 走訪 array、string

- for…in 走訪 object KEYs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

array helpers chainning

A

str.split(‘’)、arr.join(‘’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

the pattern of reducer

A

many => 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

遇到問題沒辦法解?

A

想辦法轉成array先,因為array很多helper functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

reversed string

A
  1. with general array helper like str.split、array.join
  2. for loop
  3. reducer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

palindrome

A
  1. with array helper functions (reverse後是否和原來相同)
  2. with for // 背: arr[i] === arr[arr.length - i -1]
    // 或直接用想的,直接拿頭尾想不用背
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

reversed Int

A
  • tips
    • 先toString()再parseInt()
    • 用Math.sign來得到正負號
      • i.e. result * Math.sign(num)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Max Charts Problem

A

用for…of去build up a char Map

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

fizzbuzz

A

重點在於if case的先後順序

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Array Chunking

A
  • sol. 1. // 其實很直覺
    - const chunked = [ .. [..] ]
    - 一直去判斷chunked中的last
    - 1. 沒有last || last.length === size
    - 創新chunk [el]
    - 2. 在last中加入新el, last.push(el)
    • sol. 2. 使用 array.slice(from, to]
        1. while
        1. index from 0
        1. arr.slice
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Anagrams

A
  • 如果兩個字串的使用字元數相同即為anagram
    • // fun. cleanString
    • 大小寫視為相同
    • 不考慮 空白
  • tips
      1. 用 regExp 移除掉 空白
        - str.replace(/[^\w]/g, '')
        • // ^插入符號是比對前開頭,$錢字符號則是比對結尾。
        • // 比如你輸入的搜尋條件是 ^eat,那麼你會搜尋到的結果會有eat、eaten
      1. 將字串全部轉成小寫
      1. ==String Map==
  • 重點:比較
    • 先 左比右 再 右比左 ? => bad
    • 先 1. ==比Key數量== 再 2. ==比字元數量==
  • ==the best solution==
      1. 移除所有空字元、並將字串轉成小寫
    • ==key: 2. str.split(‘’).sort()==
    • ==3. 確認兩者是否 全相等==
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

every and some

A
  • tester (currentValue) : boolean
  • every(tester) // tester(currentValue)
    - every: 每一個element都通過才會true
    - 遇到element的測試為false,就不會繼續往下做
  • some(tester) // tester(currentValue)
    - some: 只要其中一個element通過測試,就會是true
    - 遇到element的測試為true,就不會繼續往下做
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

tricks

A
  • ==將數字轉成字串 (num.toString()),以利進一步將 『 字串 』轉成 『 陣列 』來操作==
    • 數字 => 字串 => 陣列 (因為陣列很多helper functions)
    • num.toString()
    • 將數字等任何東西轉成字串,以利進一步轉成陣列來操作。
    • Math.sign(num)
      • 1 : Math.sign(正數)
      • -1 : Math.sign(負數)
      • 0 : Math.sign(0)
      • NaN : Math.sign(‘dsdgdfg’)
    • parseInt($string) // 可以消除負號,超好用 !!
    • 可以消除負號!!!!
      • i.e.: parseInt('5-') // 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

處理字串問題時,可以考慮將字串轉成 『 mapping物件 』

A

可以用在

    1. strA 和 strB 是否有一樣的chars
    1. strA 是否有重複的 chars (repeated chars)
    1. Max Chars Problem
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

slice 、splice

A
  • function signatrues are the same
    - Array.prototype.slice(start[, end])
    - Array.prototype.splice(start[, end])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly