I'm done javascripting#6
Conversation
|
Nice 👍 |
There was a problem hiding this comment.
No need to name the callback function here unless specified by the drill.
var filtered = numbers.filter( function(number){
return number % 2 === 0;
});
There was a problem hiding this comment.
Oh ok nice!
On Apr 11, 2015 7:42 PM, "jaybobo" notifications@github.com 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.
There was a problem hiding this comment.
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" jmurphy@updox.com wrote:
Oh ok nice!
On Apr 11, 2015 7:42 PM, "jaybobo" notifications@github.com 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
.
There was a problem hiding this comment.
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;
});
There was a problem hiding this comment.
Okay, I get it! That makes a lot of sense. Thanks!
@jaybobo check this out!