1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
class Fraction {
constructor(numerator, denominator = 1) { if (Number.isInteger(numerator) && Number.isInteger(denominator)) { this.numerator = numerator; this.denominator = denominator; } else if (numerator instanceof Fraction || denominator instanceof Fraction) { numerator = numerator instanceof Fraction ? numerator : new Fraction(numerator, 1); denominator = denominator instanceof Fraction ? denominator : new Fraction(denominator, 1);
return numerator.clone().divide(denominator); } else { throw new TypeError('prams must be Number or Fraction'); } this.simplify(); }
static greatestCommonDivisor(num1, num2) { let lesser = Math.abs(num1); let greater = Math.abs(num2);
while (lesser !== 0) { let t = lesser; lesser = greater % lesser; greater = t; } return greater; }
clone() { return new Fraction(this.numerator, this.denominator); }
simplify() { const gcd = Fraction.greatestCommonDivisor(this.numerator, this.denominator); this.numerator = this.numerator / gcd; this.denominator = this.denominator / gcd; return this; }
toString() { return `${this.numerator}/${this.denominator}`; }
static fromString(str = '') { let arr = str.split('/').map(Number).filter(Number.isInteger); if (arr.length === 1) arr.push(1); if (arr.length !== 2) throw TypeError('params must be 2 Integer spread by "/"'); return new Fraction(arr[0], arr[1]); }
get value() { return this.numerator / this.denominator; }
static add(frac1, frac2) { frac1 = frac1 instanceof Fraction ? frac1.clone() : new Fraction(frac1); frac2 = frac2 instanceof Fraction ? frac2.clone() : new Fraction(frac2);
frac1.numerator = frac1.numerator * frac2.denominator + frac1.denominator * frac2.numerator; frac1.denominator = frac1.denominator * frac2.denominator; return frac1.simplify(); }
add(frac) { return Fraction.add(this, frac); }
static subtract(frac1, frac2) { frac1 = frac1 instanceof Fraction ? frac1.clone() : new Fraction(frac1); frac2 = frac2 instanceof Fraction ? frac2.clone() : new Fraction(frac2);
frac1.numerator = frac1.numerator * frac2.denominator - frac1.denominator * frac2.numerator; frac1.denominator = frac1.denominator * frac2.denominator; return frac1.simplify(); }
subtract(frac) { return Fraction.subtract(this, frac); }
static multiply(frac1, frac2) { frac1 = frac1 instanceof Fraction ? frac1.clone() : new Fraction(frac1); frac2 = frac2 instanceof Fraction ? frac2.clone() : new Fraction(frac2);
frac1.numerator = frac1.numerator * frac2.numerator; frac1.denominator = frac1.denominator * frac2.denominator; return frac1.simplify(); }
multiply(frac) { return Fraction.multiply(this, frac); }
static divide(frac1, frac2) { frac1 = frac1 instanceof Fraction ? frac1.clone() : new Fraction(frac1); frac2 = frac2 instanceof Fraction ? frac2.clone() : new Fraction(frac2);
frac1.numerator = frac1.numerator * frac2.denominator; frac1.denominator = frac1.denominator * frac2.numerator; return frac1.simplify(); }
divide(frac) { return Fraction.divide(this, frac); }
}
|