Finished
44//
55
66export 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();
915 }
1016
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);
1322 }
1423
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);
1729 }
1830
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);
2136 }
2237
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);
2548 }
2649
2750 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));
2953 }
3054
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 );
3375 }
3476
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);
3780 }
3881
3982 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;
41111 }
42112}
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
Test NameStatus
Raise a real number to a positive rational number
Fail
Add two positive rational numbers
Pass
Add a positive rational number and a negative rational number
Pass
Add two negative rational numbers
Pass
Add a rational number to its additive inverse
Pass
Subtract two positive rational numbers
Pass
Subtract a positive rational number and a negative rational number
Pass
Subtract two negative rational numbers
Pass
Subtract a rational number from itself
Pass
Multiply two positive rational numbers
Pass
Multiply a negative rational number by a positive rational number
Pass
Multiply two negative rational numbers
Pass
Multiply a rational number by its reciprocal
Pass
Multiply a rational number by 1
Pass
Multiply a rational number by 0
Pass
Divide two positive rational numbers
Pass
Divide a positive rational number by a negative rational number
Pass
Divide two negative rational numbers
Pass
Divide a rational number by 1
Pass
Absolute value of a positive rational number
Pass
Absolute value of a positive rational number with negative numerator and denominator
Pass
Absolute value of a negative rational number
Pass
Absolute value of a negative rational number with negative denominator
Pass
Absolute value of zero
Pass
Absolute value of a rational number is reduced to lowest terms
Pass
Raise a positive rational number to a positive integer power
Pass
Raise a negative rational number to a positive integer power
Pass
Raise a positive rational number to a negative integer power
Pass
Raise a negative rational number to an even negative integer power
Pass
Raise a negative rational number to an odd negative integer power
Pass
Raise zero to an integer power
Pass
Raise one to an integer power
Pass
Raise a positive rational number to the power of zero
Pass
Raise a negative rational number to the power of zero
Pass
Raise a real number to a negative rational number
Pass
Raise a real number to a zero rational number
Pass
Reduce a positive rational number to lowest terms
Pass
Reduce places the minus sign on the numerator
Pass
Reduce a negative rational number to lowest terms
Pass
Reduce a rational number with a negative denominator to lowest terms
Pass
Reduce zero to lowest terms
Pass
Reduce an integer to lowest terms
Pass
Reduce one to lowest terms
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.