User:B1KWikis/EditCounterOptIn.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:B1KWikis/EditCounterOptIn. |
/* Finds the lowest common multiple of two numbers */
function LcmCalculator(x, y) { // constructor function
var checkInt = function (x) { // inner function
if (x % 1 !== 0) {
throw new TypeError(x + "is not an integer"); // throw an exception
}
return x;
};
this.a = checkInt(x)
// ^ semicolons are optional
this.b = checkInt(y);
}
// The prototype of object instances created by a constructor is
// that constructor's "prototype" property.
LCMCalculator.prototype = { // object literal
constructor: LCMCalculator, // when reassigning a prototype, set the constructor property appropriately
gcd: function () { // method that calculates the greatest common divisor
// Euclidean algorithm:
var a = Math.abs(this.a), b = Math.abs(this.b), t;
if (a < b) {
// swap variables
t = b;
b = a;
a = t;
}
while (b !== 0) {
t = b;
b = a % b;
a = t;
}
// Only need to calculate GCD once, so "redefine" this method.
// (Actually not redefinition - it's defined on the instance itself,
// so that this.gcd refers to this "redefinition" instead of LCMCalculator.prototype.gcd.)
// Also, 'gcd' == "gcd", this['gcd'] == this.gcd
this['gcd'] = function() {
return a;
};
return a;
},
"lcm"/* can use strings here */: function() {
// Variable names don't collide with object properties, e.g. |lcm| is not |this.lcm|.
// not using |this.a * this.b| to avoid FP precision issues
var lcm = this.a / this.gcd() * this.b;
// Only need to calculate lcm once, so "redefine" this method.
this.lcm = function() {
return lcm;
};
return lcm;
},
toString: function () {
return "LCMCalculator: a = " + this.a + ", b = " + this.b;
}
};
// Note: Array's map() and forEach() are defined in JavaScript 1.6.
// They are are implemented in Firefox, Chrome, Opera etc. but currently not in Internet Explorer.
// They are used here to demonstrate JavaScript's inherent functional nature.
[[25, 55],[21, 56],[22, 58],[28, 56]].map(function(pair) { // array literal + mapping function
return new LCMCalculator(pair[0], pair[1]);
}).sort(function(a, b) { // sort with this comparative function
return a.lcm() - b.lcm();
}).forEach(function(obj) {
/* Note: print() is a JS builtin function available in Mozilla's js CLI;
* It is functionally equivalent to Java's System.out.println().
* Within a web browser, print() is a very different function
* (opens the "Print Page" dialog),
* so use something like document.write() or alert() instead.
*/
// print (obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm());
// alert (obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm());
document.write(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm() + "<br>");
});
//Wikipedia's advanced example on wikipedia's page on javascript