zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Falsy Bouncer #301

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

挑战

移除所有的falsy字符。

falsy字符包括false, null, 0, "", undefined, NaN

代码


function bouncer(arr) {
  // Don't show a false ID to this bouncer.

  var arrRemove=arr.filter(function(val){return val;});

  return arrRemove;
}

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

结果显示

image

帮助

Boolean Array.prototype.filter()

来源

https://www.freecodecamp.org/challenges/falsy-bouncer

zilongxuan001 commented 6 years ago

20180330

一开始没有在函数里加return,结果老是黑屏,出不来结果。

zilongxuan001 commented 6 years ago

falsenull0""undefinedNaN都是falsy value,就是.filter会自动认定为false,而予以删除。

zilongxuan001 commented 6 years ago

20180403 .filter(function(){return })可以自动筛选数组,去掉falsy元素。