diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2012-03-25 01:11:46 +0400 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2012-03-25 01:11:46 +0400 |
commit | 5782cfb5d6705a2ab70a9f950bfca3d4d81c16e4 (patch) | |
tree | 4aefa3c52f99e91b72b5d33fd928ba9a2eff1924 | |
parent | bae7f4675fcfeda40b3c6bb1752f8fce12be5ab0 (diff) | |
download | gcd-5782cfb5d6705a2ab70a9f950bfca3d4d81c16e4.tar.gz |
JavaScript via Node.js
-rwxr-xr-x | gcd.js | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -0,0 +1,19 @@ +#!/usr/bin/node + + +function gcd2(a, b) { + return b == 0 ? a : gcd2(b, a % b); +} + +// Don't use parseInt() itself in map() - it used to have 2 arguments +var nums = process.argv.map( + function(s) {return parseInt(s)} + ).filter( // It will not work if this script or node itself is named as number ;-) + function(t) {return !isNaN(t)} + ); + +if (nums.length > 0) { + var GCD = nums.reduce(gcd2); // Yeah, we could use lambda here + console.log(GCD); +} + |