I don’t want to use “filter()” function to complete the challenge, so I wrote code in another way to solve the problem, but I got stuck.
here is my code:
/***********************************************************************/
function bouncer(arr) {
// Don't show a false ID to this bouncer.
var newArr = [];
for(var j = 0; j < arr.length;j++){
newArr.push(arr[j]);
}
var sign =0;
for (var i = 0; i < arr.length; i++ ){
console.log("i= "+i);
console.log("sing= " + sign);
console.log("length= "+arr.length);
switch(arr[i]){
case false:
newArr.splice(i-sign,1);
sign ++;
break;
case null:
newArr.splice(i-sign,1);
console.log("nullnull");
sign ++;
break;
case 0:
newArr.splice(i-sign,1);
sign ++;
break;
case "":
newArr.splice(i-sign,1);
sign++;
break;
case undefined:
newArr.splice(i-sign,1);
sign++;
break;
case isNaN(arr[i]):
newArr.splice(i-sign,1);
console.log("NaNNaN");
sign++;
break;
default:
break;
}
}
return newArr;
}
bouncer([false, null, 0, undefined, "", NaN]);
/*********************************************************************/
the result was [null] instead of [].
I have two questions:
1.why “null” was still kept in “newArr”?
2.I also tried to run the code in Chrome, but the result was different. The result in Chrome is “[NaN]”.
As a result, I was totally confused by the code.
Please give me some help! Thanks!