LarryBattle / Ratio.js

Rational numbers for Javascript
http://larrybattle.github.com/Ratio.js/
MIT License
113 stars 9 forks source link

Added setter and getter for the numerator and denominator #37

Closed LarryBattle closed 11 years ago

LarryBattle commented 11 years ago

Added setter and getter for the numerator and denominator. Also change this.numerator to this.n and this.denominator to this.d. This should also includes the parameter names. top-> n and bottom -> d

Ratio.js

numerator : function(val){
    if(val != null){
        this.n = Ratio.getNumeratorWithSign(val, this.d);
    }
    return this.n;
},
denominator : function(val){
    if(val != null){
        this.n *= (0 < +val) ? 1 : -1;
        this.d = Math.abs(val);
    }
    return this.d;
},

Test cases

test("test Ratio.prototype.numerator()", function () {
    var fn = function(a,b,c){
        var obj = new Ratio(a,b);
        obj.numerator(c);
        return obj;
    };
    equal(fn(1,2,2).toString(), "2/2");
});
test("test changing denominator", function () {
    var fn = function(a,b,c){
        var obj = new Ratio(a,b);
        obj.denominator(c);
        return obj;
    };
    equal(fn(1,3,2).toString(), "1/2");
    equal(fn(-1,3,-2).toString(), "1/2");
    equal(fn(1,3,-2).toString(), "-1/2");
    equal(fn(-1,3,2).toString(), "-1/2");
});
LarryBattle commented 11 years ago

Added in Version 0.4