| 1 |
/*! This file exists only for testing a Minify installation. Its content is not used. |
| 2 |
* |
| 3 |
* http://example.org/min/f=min/quick-test.js |
| 4 |
*/ |
| 5 |
|
| 6 |
/* Finds the lowest common multiple of two numbers */ |
| 7 |
function LCMCalculator(x, y) { // constructor function |
| 8 |
var checkInt = function (x) { // inner function |
| 9 |
if (x % 1 !== 0) { |
| 10 |
throw new TypeError(x + " is not an integer"); // throw an exception |
| 11 |
} |
| 12 |
return x; |
| 13 |
}; |
| 14 |
this.a = checkInt(x); |
| 15 |
// ^ semicolons are optional |
| 16 |
this.b = checkInt(y); |
| 17 |
} |
| 18 |
// The prototype of object instances created by a constructor is |
| 19 |
// that constructor's "prototype" property. |
| 20 |
LCMCalculator.prototype = { // object literal |
| 21 |
constructor: LCMCalculator, // when reassigning a prototype, set the constructor property appropriately |
| 22 |
gcd: function () { // method that calculates the greatest common divisor |
| 23 |
// Euclidean algorithm: |
| 24 |
var a = Math.abs(this.a), b = Math.abs(this.b), t; |
| 25 |
if (a < b) { |
| 26 |
// swap variables |
| 27 |
t = b; |
| 28 |
b = a; |
| 29 |
a = t; |
| 30 |
} |
| 31 |
while (b !== 0) { |
| 32 |
t = b; |
| 33 |
b = a % b; |
| 34 |
a = t; |
| 35 |
} |
| 36 |
// Only need to calculate GCD once, so "redefine" this method. |
| 37 |
// (Actually not redefinition - it's defined on the instance itself, |
| 38 |
// so that this.gcd refers to this "redefinition" instead of LCMCalculator.prototype.gcd.) |
| 39 |
// Also, 'gcd' === "gcd", this['gcd'] === this.gcd |
| 40 |
this['gcd'] = function () { |
| 41 |
return a; |
| 42 |
}; |
| 43 |
return a; |
| 44 |
}, |
| 45 |
// Object property names can be specified by strings delimited by double (") or single (') quotes. |
| 46 |
"lcm" : function () { |
| 47 |
// Variable names don't collide with object properties, e.g. |lcm| is not |this.lcm|. |
| 48 |
// not using |this.a * this.b| to avoid FP precision issues |
| 49 |
var lcm = this.a / this.gcd() * this.b; |
| 50 |
// Only need to calculate lcm once, so "redefine" this method. |
| 51 |
this.lcm = function () { |
| 52 |
return lcm; |
| 53 |
}; |
| 54 |
return lcm; |
| 55 |
}, |
| 56 |
toString: function () { |
| 57 |
return "LCMCalculator: a = " + this.a + ", b = " + this.b; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
//define generic output function; this implementation only works for web browsers |
| 62 |
function output(x) { |
| 63 |
document.write(x + "<br>"); |
| 64 |
} |
| 65 |
|
| 66 |
// Note: Array's map() and forEach() are defined in JavaScript 1.6. |
| 67 |
// They are used here to demonstrate JavaScript's inherent functional nature. |
| 68 |
[[25, 55], [21, 56], [22, 58], [28, 56]].map(function (pair) { // array literal + mapping function |
| 69 |
return new LCMCalculator(pair[0], pair[1]); |
| 70 |
}).sort(function (a, b) { // sort with this comparative function |
| 71 |
return a.lcm() - b.lcm(); |
| 72 |
}).forEach(function (obj) { |
| 73 |
output(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm()); |
| 74 |
}); |