Arithmetic/Rational/JavaScript: Difference between revisions

Content added Content deleted
m (prepare for inclusion as template)
 
Line 1: Line 1:
{{collection|Rational Arithmetic}}
<noinclude>{{collection|Rational Arithmetic}}</noinclude>
;The core of the Rational class

===The core of the Rational class===
<lang javascript>// the constructor
<lang javascript>// the constructor
function Rational(numerator, denominator) {
function Rational(numerator, denominator) {
Line 158: Line 157:
return !(this.lt(rat));
return !(this.lt(rat));
}</lang>
}</lang>
;Testing

===Testing===
<lang javascript>function assert(cond, msg) { if (!cond) throw msg; }
<lang javascript>function assert(cond, msg) { if (!cond) throw msg; }


Line 240: Line 238:
print('all tests passed');</lang>
print('all tests passed');</lang>


===Find perfect numbers===
;Finding perfect numbers
<lang javascript>function factors(num) {
<lang javascript>function factors(num) {
var factors = new Array();
var factors = new Array();
Line 274: Line 272:
if (isPerfect(n))
if (isPerfect(n))
print("perfect: " + n);</lang>
print("perfect: " + n);</lang>
{{out}}

which outputs
<pre>perfect: 6
<pre>perfect: 6
perfect: 28
perfect: 28