0

The following javascript code works:

const numbers = [10, 15, 20, 25, 30];

function nesting(array, iteratorFunction) {
  let f2 = function(b){
        return false;
      }
  return array.filter(
    function(a){
      return f2(a)
    }   
   );
}

The result is [] when f2 returns false, and [10, 15, 20, 25, 30] if f2 is changed to return true.

Why doesn't it work correctly after this direction substitution where I simply replaced the variable f2 with the original definition of f2?

function nesting(array, iteratorFunction) {
  return array.filter(
    function(a){
      return function(b){
        return false;
      }
    }   
   );
}

It returns [10, 15, 20, 25, 30] whether the inner function returns true or false. Why? What piece of knowledge of javascript am I missing?

BugBuddy
  • 496
  • 2
  • 4
  • 16
  • 1
    You need to auto call the b function: `return (function(b){return false))();` – kemicofa ghost Jan 09 '19 at 20:33
  • The inner function which returns the boolean is never called. In your first example you call `f2`, in your second example you return the function itself (comparable to `return f2` instead of `return f2(a)`). – tkausl Jan 09 '19 at 20:34

2 Answers2

1

The codes are not equivalent.

In the first example, you're calling f2, then using what it returns to filter.

In the second example, you never call the function; you're returning the uncalled function in the filtering function. Since functions themselves are truthy, it allows all elements, regardless of what the function returns.

To make it equivalent, you need to call the inner function:

. . . 
function(a){
  return (function(b){
    return false;
  })(a); // Add parenthesis to call
}
. . . 
Carcigenicate
  • 35,934
  • 8
  • 48
  • 81
  • How do I fix it so that the function is called in the second example? – BugBuddy Jan 09 '19 at 20:36
  • @BugBuddy Updated. – Carcigenicate Jan 09 '19 at 20:39
  • Is the use of a + sign applicable here, as in this answer? https://stackoverflow.com/a/13341710/10783877 (I assume it is, and I'll try it myself after lunch.) – BugBuddy Jan 09 '19 at 20:47
  • @BugBuddy I'm not sure of the precedence off the top of my head. It depends on if function calls or `+` bind tighter. I'd just use explicit parenthesis though unless that 1 byte that you save is actually important; like if you're golfing. – Carcigenicate Jan 09 '19 at 20:55
  • This is a good article on the topic: http://benalman.com/news/2010/11/immediately-invoked-function-expression/ – BugBuddy Jan 09 '19 at 21:00
  • 1
    @BugBuddy Actually, scratch that. `return` would already indicate that it's a function expression, so `+` would be unnecessary, or even invalid in this case. I added the parenthesis because that's what I prefer and have seen written, but I wouldn't be surprised if it's a syntax error without them. – Carcigenicate Jan 09 '19 at 21:02
  • 1
    @BugBuddy It actually is valid without the wrapping parenthesis. I still prefer them though. I find it helps me parse it easier. – Carcigenicate Jan 09 '19 at 21:15
1

In the second snippet you never execute the f2 function

function nesting(array, iteratorFunction) {
  return array.filter(
    function(a){
      return function(b){
        return false;
      }(a)
    }   
   );
}
cody
  • 467
  • 4
  • 18