Skip to content

Conversation

@murphyjaron
Copy link

@jaybobo check this out!

@jaybobo
Copy link
Member

jaybobo commented Apr 11, 2015

Nice 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to name the callback function here unless specified by the drill.

var filtered = numbers.filter( function(number){
  return number % 2 === 0;
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok nice!
On Apr 11, 2015 7:42 PM, "jaybobo" [email protected] wrote:

In javascripting/array-filtering.js
#6 (comment)
:

@@ -0,0 +1,6 @@
+var numbers = [1,2,3,4,5,6,7,8,9,10];
+var filtered = numbers.filter(function evenNumbers(number){

No need to name the callback function here unless specified by the drill.

var filtered = numbers.filter( function(number){
return number % 2 === 0;
});


Reply to this email directly or view it on GitHub
https://github.com/paircolumbus/javascripting101/pull/6/files#r28199361.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it have something to do with possibly editing the array within the for
loop without affecting the limit of the for loop?
On Apr 11, 2015 7:44 PM, "Jaron Murphy" [email protected] wrote:

Oh ok nice!
On Apr 11, 2015 7:42 PM, "jaybobo" [email protected] wrote:

In javascripting/array-filtering.js
#6 (comment)
:

@@ -0,0 +1,6 @@
+var numbers = [1,2,3,4,5,6,7,8,9,10];
+var filtered = numbers.filter(function evenNumbers(number){

No need to name the callback function here unless specified by the drill.

var filtered = numbers.filter( function(number){
return number % 2 === 0;
});


Reply to this email directly or view it on GitHub
https://github.com/paircolumbus/javascripting101/pull/6/files#r28199361
.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly, JS like some other languages embraces the idea of anonymous functions or lambdas which are functions without identifiers. They can be passed around like normal variables. So in the case where maybe you're passing in some callback function that's really large, you can just use an anonymous function instead.

This would have also worked...

function evenNumbers(num) {
  //lets say your doing a bunch of 
  //other stuff here.
  return n % 2 == 0;
}

numbers.filter(evenNumbers)

but because your function body is small, this could be preferable.

numbers.filter(function(num) {
  return n % 2 == 0;
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I get it! That makes a lot of sense. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants