Go to:
Gentoo Home
Documentation
Forums
Lists
Bugs
Planet
Store
Wiki
Get Gentoo!
Gentoo's Bugzilla – Attachment 353472 Details for
Bug 451032
Example files for Emacs test plans
Home
|
New
–
[Ex]
|
Browse
|
Search
|
Privacy Policy
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
Example file for app-emacs/javascript
lcm.js (text/plain), 2.72 KB, created by
Ulrich Müller
on 2013-07-16 20:40:53 UTC
(
hide
)
Description:
Example file for app-emacs/javascript
Filename:
MIME Type:
Creator:
Ulrich Müller
Created:
2013-07-16 20:40:53 UTC
Size:
2.72 KB
patch
obsolete
>/* 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 an exception > throw new TypeError(x + " is not an integer"); > } > 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 > // when reassigning a prototype, set the constructor property appropriately > constructor: LCMCalculator, > 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. >// array literal + mapping function >[[25, 55], [21, 56], [22, 58], [28, 56]].map(function (pair) { > 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()); >});
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 451032
:
334872
|
334874
|
334876
|
334878
|
334880
|
334882
|
334884
|
334886
|
334888
|
334890
|
334892
|
334894
|
334896
|
334898
|
334900
|
334902
|
334904
|
334906
|
334908
|
334910
|
334912
|
334914
|
334916
|
334918
|
334920
|
334922
|
334924
|
334926
|
334928
|
334930
|
334932
|
334934
|
334936
|
334938
| 353472 |
353474
|
353476
|
360484
|
370044