📜  Falsy Bouncer - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:50.856000             🧑  作者: Mango

代码示例1
// Falsy Bouncer
// Remove all falsy values from an array.
// Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
// Hint: Try converting each value to a Boolean.

function bouncer(arr) {
    let newArray = [];
    for (let i = 0; i < arr.length; i++) {
        if (arr[i]) newArray.push(arr[i]);
    }
    return newArray;
}

// OR

function bouncer(arr) {
    return arr.filter(Boolean);
}

bouncer([7, 'ate', '', false, 9]);