diff options
author | Vincent Sanders <vince@netsurf-browser.org> | 2012-11-06 18:10:29 +0000 |
---|---|---|
committer | Vincent Sanders <vince@netsurf-browser.org> | 2012-11-06 18:20:41 +0000 |
commit | 16c105972399ad8f55bdfeec84ace9633552173f (patch) | |
tree | 87cace4ff8108fbb8b248afc380e91e40d71cdf1 /test | |
parent | 12f874cd9260a1866948cb54874e7c9d80225714 (diff) | |
download | netsurf-16c105972399ad8f55bdfeec84ace9633552173f.tar.gz netsurf-16c105972399ad8f55bdfeec84ace9633552173f.tar.bz2 |
add another javascript test
Diffstat (limited to 'test')
-rw-r--r-- | test/js/index.html | 4 | ||||
-rw-r--r-- | test/js/wikipedia-lcm.html | 9 | ||||
-rw-r--r-- | test/js/wikipedia-lcm.js | 70 |
3 files changed, 83 insertions, 0 deletions
diff --git a/test/js/index.html b/test/js/index.html index f618e93c5..9df56d0c8 100644 --- a/test/js/index.html +++ b/test/js/index.html @@ -23,5 +23,9 @@ <li><a href="doc-dom1.html">Direct reference</a></li> <li><a href="doc-dom2.html">getElementById reference</a></li> </ul> +<h2>Assorted</a> +<ul> +<li><a href="wikipedia-lcm.html">Example from wikipedia</a></li> +</ul> </body> </html> diff --git a/test/js/wikipedia-lcm.html b/test/js/wikipedia-lcm.html new file mode 100644 index 000000000..e91cc6182 --- /dev/null +++ b/test/js/wikipedia-lcm.html @@ -0,0 +1,9 @@ +<html> +<head> +<title>Wikipedia lcm script</title> +</head> +<body> +<h1>Wikipedia lcm script</h1> +<script src="wikipedia-lcm.js"></script> +</body> +</html> diff --git a/test/js/wikipedia-lcm.js b/test/js/wikipedia-lcm.js new file mode 100644 index 000000000..5c7ab5530 --- /dev/null +++ b/test/js/wikipedia-lcm.js @@ -0,0 +1,70 @@ +/* Finds the lowest common multiple (LCM) 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; + }, + // Object property names can be specified by strings delimited by double (") or single (') quotes. + "lcm" : 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; + } +}; + +//define generic output function; this implementation only works for web browsers +function output(x) { + document.body.appendChild(document.createTextNode(x)); + document.body.appendChild(document.createElement('br')); +} + +// Note: Array's map() and forEach() are defined in JavaScript 1.6. +// 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) { + output(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm()); +}); |