| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class Rational { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor(numerator, denominator) { |
| 8 | if (denominator === 0) { |
| 9 | throw new Error('Denominator cannot be zero'); |
| 10 | } |
| 11 | |
| 12 | this.numerator = numerator; |
| 13 | this.denominator = denominator; |
| 14 | this.reduce(); |
| 9 | 15 | } |
| 10 | 16 | |
| 11 | | add() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 17 | add(other) { |
| 18 | // Edge Case: Adding two rational numbers |
| 19 | const newNumerator = this.numerator * other.denominator + other.numerator * this.denominator; |
| 20 | const newDenominator = this.denominator * other.denominator; |
| 21 | return new Rational(newNumerator, newDenominator); |
| 13 | 22 | } |
| 14 | 23 | |
| 15 | | sub() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 24 | sub(other) { |
| 25 | // Edge Case: Subtracting two rational numbers |
| 26 | const newNumerator = this.numerator * other.denominator - other.numerator * this.denominator; |
| 27 | const newDenominator = this.denominator * other.denominator; |
| 28 | return new Rational(newNumerator, newDenominator); |
| 17 | 29 | } |
| 18 | 30 | |
| 19 | | mul() { |
| 20 | | throw new Error('Remove this line and implement the function'); |
| 31 | mul(other) { |
| 32 | // Edge Case: Multiplying two rational numbers |
| 33 | const newNumerator = this.numerator * other.numerator; |
| 34 | const newDenominator = this.denominator * other.denominator; |
| 35 | return new Rational(newNumerator, newDenominator); |
| 21 | 36 | } |
| 22 | 37 | |
| 23 | | div() { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 38 | div(other) { |
| 39 | // Edge Case: Division by zero rational number |
| 40 | if (other.numerator === 0) { |
| 41 | throw new Error('Division by zero'); |
| 42 | } |
| 43 | |
| 44 | // Edge Case: Dividing by another rational number |
| 45 | const newNumerator = this.numerator * other.denominator; |
| 46 | const newDenominator = this.denominator * other.numerator; |
| 47 | return new Rational(newNumerator, newDenominator); |
| 25 | 48 | } |
| 26 | 49 | |
| 27 | 50 | abs() { |
| 28 | | throw new Error('Remove this line and implement the function'); |
| 51 | // Edge Case: Absolute value of a rational number |
| 52 | return new Rational(Math.abs(this.numerator), Math.abs(this.denominator)); |
| 29 | 53 | } |
| 30 | 54 | |
| 31 | | exprational() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 55 | exprational(n) { |
| 56 | // Edge Case: Raising to power 0 |
| 57 | if (n === 0) { |
| 58 | return new Rational(1, 1); |
| 59 | } |
| 60 | |
| 61 | // Edge Case: Raising to a negative power |
| 62 | if (n < 0) { |
| 63 | const absN = Math.abs(n); |
| 64 | return new Rational( |
| 65 | Math.pow(this.denominator, absN), |
| 66 | Math.pow(this.numerator, absN) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | // Edge Case: Raising to a positive power |
| 71 | return new Rational( |
| 72 | Math.pow(this.numerator, n), |
| 73 | Math.pow(this.denominator, n) |
| 74 | ); |
| 33 | 75 | } |
| 34 | 76 | |
| 35 | | expreal() { |
| 36 | | throw new Error('Remove this line and implement the function'); |
| 77 | expreal(base) { |
| 78 | // Edge Case: Exponentiation of a real number to a rational number |
| 79 | return Math.pow(base, this.numerator / this.denominator); |
| 37 | 80 | } |
| 38 | 81 | |
| 39 | 82 | reduce() { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 83 | // Edge Case: Zero numerator |
| 84 | if (this.numerator === 0) { |
| 85 | this.denominator = 1; |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | // Edge Case: Negative denominator |
| 90 | if (this.denominator < 0) { |
| 91 | this.numerator *= -1; |
| 92 | this.denominator *= -1; |
| 93 | } |
| 94 | |
| 95 | // Edge Case: Reducing to lowest terms |
| 96 | const gcd = this.gcd(Math.abs(this.numerator), Math.abs(this.denominator)); |
| 97 | this.numerator = this.numerator / gcd; |
| 98 | this.denominator = this.denominator / gcd; |
| 99 | |
| 100 | return this; |
| 101 | } |
| 102 | |
| 103 | gcd(a, b) { |
| 104 | // Edge Case: Calculating greatest common divisor |
| 105 | while (b !== 0) { |
| 106 | const temp = b; |
| 107 | b = a % b; |
| 108 | a = temp; |
| 109 | } |
| 110 | return a; |
| 41 | 111 | } |
| 42 | 112 | } |
| 113 | // Handled Edge Cases: zero denominator, negative denominators, zero numerator with non-zero denominator, both numerator and denominator zero, reduction to lowest terms, division by zero in rational operations, raising to power 0, raising to negative power, raising to positive power |