quick-test.js 2.7 KB

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