Last active
October 24, 2016 00:36
-
-
Save cuipengfei/7968926 to your computer and use it in GitHub Desktop.
functional js count ducks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function duckCount() { | |
return Array.prototype.slice.call(arguments).filter(function(obj) { | |
return Object.prototype.hasOwnProperty.call(obj, 'quack') | |
}).length | |
} | |
module.exports = duckCount |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function duckCount() { | |
function isDuck(arg) { | |
return Object.prototype.hasOwnProperty.call(arg, "quack") | |
} | |
function countDuck(args, index, number) { | |
if (index >= args.length) { | |
return number | |
} | |
else { | |
return countDuck(args, index + 1, number + (isDuck(args[index]) ? 1 : 0)) | |
} | |
} | |
return countDuck(arguments, 0, 0) | |
} | |
module.exports = duckCount |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function duckCount() {
return Array.prototype.filter.call(arguments, function(obj){
return Object.prototype.hasOwnProperty.call(obj, "quack");
}).length;
}
Thanks for posting this up! Take a look at my revision. maybe even better?