Falsy is something which evaluates to FALSE. There are only six falsy values in JavaScript: undefined, null, NaN, 0, “” (empty string), and false of course.
Hint 2
We need to make sure we have all the falsy values to compare, we can know it, maybe with a function with all the falsy values…
Hint 3
Then we need to add a filter() with the falsy values function…
Solutions
Solution 1 (Click to Show/Hide)
function bouncer(arr) {
const filteredArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i]) filteredArr.push(arr[i]);
}
return filteredArr;
}
Code Explanation
We create a new empty array (filteredArr).
We use a for cycle to iterate over all elements of the provided array (arr).
We use the if statement to check if the current element is truthy or falsy.
If the element is truthy, we push it to the new array (newArray). This result in the new array (filteredArr) containing only truthy elements.
function bouncer(arr) {
return arr.filter(Boolean);
}
Code Explanation
The Array.prototype.filter method expects a function that returns a Boolean value which takes a single argument and returns true for truthy value or false for falsy value. Hence we pass the built-in Boolean function.
function bouncer(arr) {
//Only return values that evaluate as true inside the array.
return arr.filter(function(value){
if (value){
return (value);
}
});
}
function bouncer(arr) {
function truthy(value) {
//only return the given value if it evaluates as true.
//For example, 2, "string", true, will all return, but false, NaN, -1 will not.
return value;
}
var filtered = arr.filter(truthy);
//This is filtering only the values that evaluate as true in the given array (following the truthy function give above)
return filtered;
}
//The function is now run with a given array, returning only the values that evaluate as true
bouncer([7, "ate", "", false, 9]);
My solution was a little longer, but it made a lot more sense to me and actually didn’t take long at all.
My code is pasted below, but essentially, I made a loop which would go through each element of the array and put it through a boolean operator to see if it was true. If it’s true, then push that element to a new array called noFalsy. Then, it filters the new noFalsy array for any values of null, because the nature of this particular loop means that if you have items in the middle of your array that are false but perhaps one at the end that is true, all of those false values will become null values in the new array.
function bouncer(arr) {
var noFalsy = [];
for (i = 0; i < arr.length; i++) {
var x = Boolean(arr[i]);
if (x === true) {
noFalsy[i] = arr[i];
} else {
}
}
//filters function and removes any items that are null
var noFalsyFinal = noFalsy.filter(function(noFalsy){
return noFalsy !== null;
});
return noFalsyFinal;
}
bouncer([7, "ate", "", false, 9]);
function bouncer(arr) {
// Don't show a false ID to this bouncer.
var newArr = arr.filter(function(element){
if(Boolean(element)){
return element;
}
});
return newArr;
}
Boolean(x) means that we take the value and transform it to boolean, as a result get true or false.