W3Cschool初级脚本算法(14.删除数组中特定值算法挑战)

删除数组中特定值算法挑战


问题:

删除数组中的所有的假值。

在JavaScript中,假值有falsenull0""undefinedNaN


要求:

bouncer([7, "ate", "", false, 9]) 应该返回 [7, "ate", 9].

bouncer(["a", "b", "c"]) 应该返回 ["a", "b", "c"].

bouncer([false, null, 0, NaN, undefined, ""]) 应该返回 [].

bouncer([1, null, NaN, 2, undefined]) 应该返回 [1, 2]


问题答案:

function bouncer(arr) {
// Don't show a false ID to this bouncer.
  return arr.filter(function(val){
    return !(!val || val === "");
  });
}

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

题目链接:

https://www.w3cschool.cn/codecamp/falsy-bouncer.html

猜你喜欢

转载自blog.csdn.net/qq_42044073/article/details/82491569