Sha256: 33b866c63792696cdf8d864e3c76bdd4b648a34d521a6fd35ea029307d859657

Contents?: true

Size: 1.94 KB

Versions: 68

Compression:

Stored size: 1.94 KB

Contents

function Rational(numerator, denominator) {
  if (denominator === 0) {throw new Error('Denominator must not be zero.');}

  this.numerator = numerator;
  this.denominator = denominator;

  this.reduce();
  this.ensureSignInNumerator();
}

Rational.prototype.add = function (that) {
  var commonDenominator = this.denominator * that.denominator;
  return new Rational(this.numerator * that.denominator + that.numerator * this.denominator, commonDenominator);
};

Rational.prototype.sub = function (that) {
  var commonDenominator = this.denominator * that.denominator;
  return new Rational(this.numerator * that.denominator - that.numerator * this.denominator, commonDenominator);
};

Rational.prototype.mul = function (that) {
  return new Rational(this.numerator * that.numerator, this.denominator * that.denominator);
};

Rational.prototype.div = function (that) {
  return new Rational(this.numerator * that.denominator, this.denominator * that.numerator);
};

Rational.prototype.abs = function () {
  return new Rational(Math.abs(this.numerator), Math.abs(this.denominator));
};

Rational.prototype.exprational = function (n) {
  return new Rational(Math.pow(this.numerator, n), Math.pow(this.denominator, n));
};

Rational.prototype.expreal = function (base) {
  return Math.pow(10.0, Math.log10(Math.pow(base, this.numerator)) / this.denominator);
};

Rational.prototype.reduce = function () {
  var commonDivisor = this.gcd(this.numerator, this.denominator);

  this.numerator /= commonDivisor;
  this.denominator /= commonDivisor;
  this.ensureSignInNumerator();

  return this;
};

Rational.prototype.gcd = function (a, b) {
  var localA = a;
  var localB = b;
  while (localB !== 0) {
    var t = localB;
    localB = localA % localB;
    localA = t;
  }
  return localA;
};

Rational.prototype.ensureSignInNumerator = function () {
  if (this.denominator < 0) {
    this.denominator = -this.denominator;
    this.numerator = -this.numerator;
  }
};

module.exports = Rational;

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.179 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.178 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.177 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.176 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.175 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.174 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.173 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.172 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.171 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.170 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.169 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.167 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.166 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.165 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.164 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.163 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.162 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.161 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.160 tracks/javascript/exercises/rational-numbers/example.js