Tell us what’s happening:
Describe your issue in detail here.
i use the follow code to test the result.
The test result is [ false, 9 ] which eliminate [7, “ate”].
I am not sure which part is wrong. Can anyone help me?
**Your code so far**
function bouncer(arr) {
for(let i=0;i<arr.length;i++){
let n=i+1;
if(arr[i]===false||arr[i]===null||arr[i]===0||arr[i]=== ""||arr[i]===undefined||arr[i]===NaN){
arr.splice(0,n);
}
}
return arr;
}
console.log(bouncer([7, "ate", "", false, 9]));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
Challenge: Falsy Bouncer
Link to the challenge:
I think you are making this a little too complicated. As @JeremyLT said, there is a much easier way to check for “falsy” values in JS. If you aren’t sure about that then a little googling will probably give you what you need.
I can see why you are modifying the original array passed into the function. I don’t think the instructions are quite clear enough. They should say, “Return a new array with all ‘falsy’ values removed from the array passed in”. It also doesn’t help that the default code has
return arr;
in the body of the function.
Tell us what’s happening:
Describe your issue in detail here.
i use the follow code to test the result.
The result shall be but now appears [ NaN ].
I have added the [ NaN ] scenario in the if statement.
Why still appear?
**Your code so far**
function bouncer(arr) {
let newarr=[];
for(let i=0;i<arr.length;i++){
if(arr[i]!==NaN&&arr[i]!==null&&arr[i]!==0&&arr[i]!== ""&&arr[i]!==undefined&&arr[i]!==false){
newarr.push(arr[i]);
}
}
return newarr;
}
console.log(bouncer([false, null, 0, NaN, undefined, ""]));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
Challenge: Falsy Bouncer
Link to the challenge:
This isn’t any better. You cannot directly compare against some falsy values.
You need to use the idea behind falsy and truthy values.
What happens if you run this code:
if (5)
console.log("5 is truthy");
else
console.log("5 is falsy");
if (0)
console.log("0 is truthy");
else
console.log("0 is falsy");
Hi @alexllooyyoo !
Welcome to the forum!
I think reviewing the definition for if statements will help you.
MDN Docs:
The if statement executes a statement if a specified condition is truthy
Hopefully that helps you simplify this if statement.