bedward
November 19, 2020, 6:13pm
1
Tell us what’s happening:
NOTE: i don’t want someone to show me the solution, i want to know why it is not working please!
I don’t understand why this code below is not working as expected
Your code so far
function bouncer(arr) {
for (let val in arr) {
if(arr[val] === false || arr[val] === null || arr[val] === 0 || arr[val] === "" || arr[val] === undefined || arr[val] === NaN ) {
arr.splice(val, 1);
}
}
return arr;
}
console.log(bouncer([false, null, 0, NaN, undefined, ""]));
Challenge: Falsy Bouncer
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
You don’t want to check if your value is on some special ‘falsy’ list. This misses the idea of ‘truthy’ and ‘falsy’.
Try this code
if (1)
console.log("truthy")
else
console.log("falsy")
if (0)
console.log("truthy")
else
console.log("falsy")
if (NaN)
console.log("truthy")
else
console.log("falsy")
if ("hello")
console.log("truthy")
else
console.log("falsy")
bedward
November 19, 2020, 6:17pm
3
But i don’t understand why the code is not working?
For some falsy values, you cannot check against them.
let badOne = Math.sqrt(-1);
let badTwo = undefined + undefined;
should those two values be strictly equal to each other?
This approach won’t work because NaN === NaN
is false. And it misses the power of the idea of ‘truthy’ and ‘falsy’.
1 Like
Let me explain you the “Why” part.
function splice
modifies the original array.
Therefore. When you iterate your array in loop, you iterate elements indexes:
First iteration. val = 0
.
from array [false, null, 0, NaN, undefined, ""]
you eliminate false
value + modify the array. It becomes: [null, 0, NaN, undefined, ""]
Next iteration val = 1
.
So you eliminate element arr[1]
which is 0
. Array becomes: [null, NaN, undefined, ""]
Next iteration val = 2
so you eliminate undefined
. Array becomes: [ null, NaN, '' ]
val=3
and array.length
is also 3
which means you finished iterating the array. So this is your final result: [ null, NaN, '' ]
Yes, but also you just can’t check if NaN === NaN
, so this approach just won’t work.
1 Like
bedward
November 19, 2020, 8:07pm
7
??? Why is 0 eliminated before null?
bedward
November 19, 2020, 8:09pm
8
So undefined values can’t be checked?
undefined
is different than NaN
You can’t check if two NaN
values are the same because there are a ton of ways to make a NaN
.
For the order, that is the risk of mutating an array as you loop over it. You are essentially saying
Check index 0. If it’s falsy, remove it.
Check index 1. If it’s falsy, remove it.
…
and so on.
But as soon as you remove an item, the contents all shift down an index, resulting in skiped items.
1 Like
bedward
November 19, 2020, 8:17pm
10
Are the falsy values
equivalent to the false
value (i.e. undefined === false ?)
Falsy vales are losely equivalent to false
(ie val == false
). That’s the definition of falsy.
Similarly, truthy values are losely equivalent to true
.
But…
In an if
statement,
if (condition) {...
actually is evaluated as
if (condition == true) {...
1 Like
bedward
November 19, 2020, 8:28pm
13
function bouncer(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
} else {
arr.splice(i, 1);
i--;
}
}
return arr;
}
I’ve solved the challenge using this code, but i find it kinda dumb, because of this if (arr[i]) { }
bedward
November 19, 2020, 8:32pm
14
but the following values are considered falsy values : [ null, NaN, undefined ], but are not ==false
They are indeed losely equivalent to false
. Try
console.log(NaN == false);
console.log(undefined == false);
== and === do different things
That approach works, but you can also do this by making a copy of the array that only holds truthy values. The solution looks cleaner, and I typically try to avoid directly modifying inputs.
bedward
November 19, 2020, 10:06pm
16
console.log(NaN == false); //returns false
console.log(undefined == false);//returns false
That’s idiotic. Sometimes JS doesn’t follow its own rules very well.
console.log(NaN != true); // returns true
console.log(undefined != true); // returns true
console.log(42 != true); // returns false
NaN
and undefined
are not losely equivalent to true
, which means they should be losely equivalent to false
, but
bedward
November 19, 2020, 10:11pm
18
so does that mean that : (NaN != true ) != (NaN == false)?
Yeah, sometimes the symbolic logic doesn’t line up quite right.