Arithmetic/Rational/JavaScript: Difference between revisions

Content added Content deleted
m (add JavaScript)
 
m (formatting)
Line 1: Line 1:
{{collection|Rational Arithmetic}}
{{collection|Rational Arithmetic}}


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

////////////////////////////////////////////////////////////


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


print('testing')
print('testing')
Line 238: Line 238:




print('all tests passed')
print('all tests passed');</lang>


===Find perfect numbers===
////////////////////////////////////////////////////////////
<lang javascript>function factors(num) {
//
function factors(num) {
var factors = new Array();
var factors = new Array();
var sqrt = Math.floor(Math.sqrt(num));
var sqrt = Math.floor(Math.sqrt(num));