Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
40 changes: 31 additions & 9 deletions fraction.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import { gcdBruteForce } from "./gcd.ts";
import { roundTo } from "./utils.ts";
import { gcdEuclid } from "./gcd.ts";



export class Fraction {
constructor(
private numerator: number,
private denominator: number,
) {}
public numerator: number,
public denominator: number,
) {
if (denominator === 0) throw new Error("Denominator cannot be zero");
const gcdValue = gcdEuclid(numerator, denominator);
this.numerator = numerator / gcdValue;
this.denominator = denominator / gcdValue;


public add(other: Fraction) {
const newNumerator =
this.numerator * other.denominator + other.numerator * this.denominator;
const newDenominator = this.denominator * other.denominator;
this.numerator = newNumerator;
this.denominator = newDenominator;
}


public cancel(): Fraction {
const gcd = gcdBruteForce(this.numerator, this.denominator);
this.numerator = this.numerator / gcd;
this.denominator = this.denominator / gcd;
return this
}

public add(other: Fraction): Fraction {
const newNumerator =
this.numerator * other.denominator + other.numerator * this.denominator;
const newDenominator = this.denominator * other.denominator;
return new Fraction(newNumerator, newDenominator);
}

public subtract(other: Fraction) {
Expand All @@ -34,6 +52,7 @@ export class Fraction {
const newDenominator = this.denominator * other.numerator;
this.numerator = newNumerator;
this.denominator = newDenominator;
return this;
}

public toFloat(precision: number): number {
Expand All @@ -54,6 +73,9 @@ export class Fraction {
if (Number.isNaN(numerator) || Number.isNaN(denominator)) {
throw new Error(`non-numeric numerator/denominator`);
}
if (denominator === 0) {
throw new Error("Denominator cannot be zero");
}
return new Fraction(numerator, denominator);
}
}
69 changes: 67 additions & 2 deletions fraction_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import { assertAlmostEquals, assertEquals } from "@std/assert";
import { Fraction } from "./fraction.ts";

const fractionTests = [
{ numerator: 2, denominator: 4, expected: "1/2" },
{ numerator: 10, denominator: 5, expected: "2/1" },
{ numerator: 36, denominator: 60, expected: "3/5" },
{ numerator: 7, denominator: 1, expected: "7/1" },
{ numerator: 0, denominator: 5, expected: "0/1" },
];

for (const { numerator, denominator, expected } of fractionTests) {
Deno.test(`Fraction(${numerator}, ${denominator}) -> ${expected}`, () => {
const frac = new Fraction(numerator, denominator);
assertEquals(frac.toString(), expected);
});
}


Deno.test("Bruch kürzen 1/1", () => {
const frac = new Fraction(1, 1);
frac.cancel();
assertEquals(frac.numerator, 1);
assertEquals(frac.denominator, 1);
});

Deno.test("Bruch kürzen 14/21 -> 2/3", () => {
const frac = new Fraction(14, 21);
frac.cancel();
assertEquals(frac.numerator, 2);
assertEquals(frac.denominator, 3);
});

Deno.test("fraction of 1/1 is 1.0", () => {
// Arrange
const fraction = new Fraction(1, 1);
Expand All @@ -12,6 +42,7 @@ Deno.test("fraction of 1/1 is 1.0", () => {
assertEquals(float, 1.0);
});


Deno.test("fraction of 2/3 is roughly 0.67", () => {
// Arrange
const fraction = new Fraction(2, 3);
Expand All @@ -29,8 +60,42 @@ Deno.test("1/3 + 2/6 = 2/3 is roughly 0.67", () => {
const right = new Fraction(2, 6);

// Act
left.add(right);
const result = left.add(right);

// Assert
assertAlmostEquals(left.toFloat(0.01), 0.67);
assertAlmostEquals(result.toFloat(0.01), 0.67);
});


Deno.test("Addition von Brüchen", () => {
const a = new Fraction(1, 2);
const b = new Fraction(1, 3);
const result = a.add(b);
assertEquals(result.toString(), "5/6");
});

Deno.test("Creating fraction with denominator 0 throws error", () => {

try {
new Fraction(3, 0);
throw new Error("Expected error was not thrown"); // Falls kein Error
} catch (e: unknown) { // ← hier
if (e instanceof Error) {
assertEquals(e.message, "Denominator cannot be zero");
} else {
throw e; // falls es etwas anderes ist
}
}


try {
Fraction.parse("3/0");
throw new Error("Expected error was not thrown");
} catch (e: unknown) { // ← hier
if (e instanceof Error) {
assertEquals(e.message, "Denominator cannot be zero");
} else {
throw e;
}
}
});
25 changes: 25 additions & 0 deletions gcd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function gcdBruteForce(a: number, b: number): number {
let gcd = 1;
const limit = Math.min(a, b);

for (let i = 1; i <= limit; i++) {
if (a % i === 0 && b % i === 0) {
gcd = i;
}
}

return gcd;
}
export function gcdEuclid(a: number, b: number): number {

while (a !== b) {
const maxNum = Math.max(a, b);
const minNum = Math.min(a, b);
const c = maxNum - minNum;
a = minNum;
b = c;
}
return a;
}


14 changes: 14 additions & 0 deletions gcd_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assertEquals } from "@std/assert";
import { gcdBruteForce } from "./gcd.ts";

Deno.test("Grösster gemeinsamer Teiler von 1 und 1", () => {
const result = gcdBruteForce(1, 1);
assertEquals(result, 1);
});

Deno.test("Grösster gemeinsamer Teiler von 6 und 9", () => {
const result = gcdBruteForce(6, 9);
assertEquals(result, 3);
});


5 changes: 5 additions & 0 deletions geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export class Point2D {
Math.abs(this.x - other.x) ** 2 + Math.abs(this.y - other.y) ** 2,
);
}

move(dx: number, dy: number) {
this.x += dx;
this.y += dy;
}
}

export class Circle implements Shape {
Expand Down
7 changes: 7 additions & 0 deletions geometry_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ Deno.test("circumference of a circle with radius 5 is roughtly 31.416", () => {
// Then
assertAlmostEquals(actual, 31.416, 0.01);
});

Deno.test("Move Point", () => {
const p = new Point2D(2, 3);
p.move(1, -1)
assertAlmostEquals(p.x, 3);
assertAlmostEquals(p.y, 2);
});