LeetCode JavaScript 30 Days Challenge – Day5 – 2634. Filter Elements from Array

    LeetCode JavaScript 30 Days ChallengeLeetCode Js-Array

    Given an integer array arr and a filtering function fn, return a new array with a fewer or equal number of elements.
    The returned array should only contain elements where fn(arr[i], i) evaluated to a truthy value.
    Please solve it without the built-in Array.filter method.

    給予一個整數陣列 arr 和一個過濾函數 fn,回傳一個數量條件少於或等於元素的新陣列。
    回傳的陣列應僅包含 fn(arr[i], i) 評估符合條且正確的元素。
    
    請不要使用 Array.filter 方法來解決問題。
    

    Example 1:

    Input: arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }
    Output: [20,30]
    

    Example 2:

    Input: arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }
    Output: [1]
    

    Example 3:

    Input: arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }
    Output: [-2,0,1,2]
    

    solution:
    因題目希望我們不要使用 filter() 的方法,同時又希望回傳的是一個新的陣列,
    所以這部分我們先宣告一個空陣列叫做 res,接著我們運用 forEach((num, index)) 的方式,
    將題目給的 arr 中所有的 element 的數值 num 與位置 index,依序放入 fn(num, index)中做運算,
    且同步運用 if條件來判斷 fn 回傳的結果是否為真值。(ex. false、undefined、null、0、NaN、””,以上皆非真值)
    最後將符合條件的真值放入到新的陣列 res 中,並回傳結果。

    Code 1: BigO(n)

    var filter = function(arr, fn) {
        const res = []
    
        arr.forEach((num, index) => {
            if (fn(num, index)) {
                res.push(num)
            }
        })
        return res;
    };
    

    FlowChart:
    Example 1

    Input: arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }
    
    if (0 > 10) res.push(0) //res = []
    if (10 > 10) res.push(10) //res = []
    if (20 > 10) res.push(20) //res = [20]
    if (30 > 10) res.push(30) //res = [20, 30]
    

    Example 2

    Input: arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }
    
    n = element
    i = index
    
    if (return 0 === 0) res.push(1) //res = [1]
    if (return 1 === 0) res.push(2) //res = [1]
    if (return 2 === 0) res.push(3) //res = [1]
    

    Example 3

    Input: arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }
    
    if (return n + 1) res.push(-2) //res = [-2]
    if (return n + 1) res.push(-1) //res = [-2] (-1 + 1 = 0, 0 為非真值, 故if(false){不執行條件內容})
    if (return n + 1) res.push(0) //res = [-2, 0]
    if (return n + 1) res.push(1) //res = [-2, 0, 1]
    if (return n + 1) res.push(2) //res = [-2, 0, 1, 2]
    

    Truthy(真值): https://developer.mozilla.org/zh-CN/docs/Glossary/Truthy

    if (-1) console.log(-1) //-1
    if (false) console.log(false) //非真值
    if (undefined) console.log(undefined) //非真值
    if (null) console.log(null) //非真值
    if (0) console.log(0) //非真值
    if (NaN) console.log(NaN) //非真值
    if ("") console.log("") //非真值