From 5782cfb5d6705a2ab70a9f950bfca3d4d81c16e4 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Sun, 25 Mar 2012 01:11:46 +0400 Subject: JavaScript via Node.js --- gcd.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100755 gcd.js diff --git a/gcd.js b/gcd.js new file mode 100755 index 0000000..8c3cd76 --- /dev/null +++ b/gcd.js @@ -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); +} + -- cgit v1.2.3