diff --git a/exercise4/problem1/README.md b/exercise4/problem1/README.md new file mode 100644 index 0000000..cecf3ca --- /dev/null +++ b/exercise4/problem1/README.md @@ -0,0 +1,14 @@ +# Problem 1 + +Create a method in the `Person` class which returns how another person's age compares. Format is **{other person name} +is {older than / younger than / the same age as} me.** + +```ts +const p1 = Person("Samuel", 24) +const p2 = Person("Joel", 36) +const p3 = Person("Lily", 24) + +p1.compareAge(p2) // "Joel is older than me." +p2.compareAge(p1) // "Samuel is younger than me." +p1.compareAge(p3) // "Lily is the same age as me." +``` \ No newline at end of file diff --git a/exercise4/problem1/index.test.ts b/exercise4/problem1/index.test.ts new file mode 100644 index 0000000..6454c78 --- /dev/null +++ b/exercise4/problem1/index.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, test } from "vitest"; +import Person from "./index"; + +describe("exercise4 - problem1", () => { + const p1 = new Person("Samuel", 24); + const p2 = new Person("Joel", 36); + const p3 = new Person("Lily", 24); + + test.each([ + { + output: p1.compareAge(p2), + expected: "Joel is older than me.", + }, + { + output: p1.compareAge(p3), + expected: "Lily is the same age as me.", + }, + { + output: p2.compareAge(p1), + expected: "Samuel is younger than me.", + }, + { + output: p2.compareAge(p3), + expected: "Lily is younger than me.", + }, + { + output: p3.compareAge(p1), + expected: "Samuel is the same age as me.", + }, + { + output: p3.compareAge(p2), + expected: "Joel is older than me.", + }, + ])(".compareAge", ({ output, expected }) => { + expect(output).toBe(expected); + }); +}); diff --git a/exercise4/problem1/index.ts b/exercise4/problem1/index.ts new file mode 100644 index 0000000..030be96 --- /dev/null +++ b/exercise4/problem1/index.ts @@ -0,0 +1,3 @@ +class Person {} + +export default Person; diff --git a/exercise4/problem2/README.md b/exercise4/problem2/README.md new file mode 100644 index 0000000..9376a15 --- /dev/null +++ b/exercise4/problem2/README.md @@ -0,0 +1,19 @@ +# Problem 2 + +Write a class called `Name` and create the following attributes given a first name and last name (as `firstName` +and `lastName`): + +* An attribute called `fullName` which returns the first and last names. +* An attribute called `initials` which returns the first letters of the first and last name. Put a `.` between the two + letters. + +Remember to allow the attributes `firstName` and `lastName` to be accessed individually as well. + +```ts +const p1 = new Name("john", "SMITH") + +p1.firstName // "John" +p1.lastName // "Smith" +p1.fullName // "John Smith" +p1.initials // "J.S" +``` \ No newline at end of file diff --git a/exercise4/problem2/index.test.ts b/exercise4/problem2/index.test.ts new file mode 100644 index 0000000..69d599e --- /dev/null +++ b/exercise4/problem2/index.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, test } from "vitest"; +import Name from "./index"; + +describe("exercise4 - problem2", () => { + test.each([ + { + firstName: "john", + lastName: "SMITH", + expected: { + firstName: "John", + lastName: "Smith", + fullName: "John Smith", + initials: "J.S", + }, + }, + { + firstName: "sARah", + lastName: "SMITH", + expected: { + firstName: "John", + lastName: "Smith", + fullName: "John Smith", + initials: "J.S", + }, + }, + ])(".Name", ({ firstName, lastName, expected }) => { + const person = new Name(firstName, lastName); + expect(person.firstName).toBe(expected.firstName); + expect(person.lastName).toBe(expected.lastName); + expect(person.fullName).toBe(expected.fullName); + expect(person.initials).toBe(expected.initials); + }); +}); diff --git a/exercise4/problem2/index.ts b/exercise4/problem2/index.ts new file mode 100644 index 0000000..dc59f55 --- /dev/null +++ b/exercise4/problem2/index.ts @@ -0,0 +1,3 @@ +class Name {} + +export default Name; diff --git a/exercise4/problem3/README.md b/exercise4/problem3/README.md new file mode 100644 index 0000000..5da8d8b --- /dev/null +++ b/exercise4/problem3/README.md @@ -0,0 +1,13 @@ +# Problem 3 + +Complete `class`: `Rectangle` and `Circle` and combine it in order to calculate sum of all areas. _Do it in OOP way_. + +```ts +const circle1 = new Circle(2); +const circle2 = new Circle(3); +const rect1 = new Rectangle(2, 4); +const rect2 = new Rectangle(3, 2); + +console.log(sumOfAllAreas(circle1, circle2)) // ~ 40. ... +console.log(sumOfAllAreas(circle1, rect1)) // ~ 20. ... +``` \ No newline at end of file diff --git a/exercise4/problem3/index.test.ts b/exercise4/problem3/index.test.ts new file mode 100644 index 0000000..a4f88e6 --- /dev/null +++ b/exercise4/problem3/index.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, test } from "vitest"; +import { Circle, Rectangle, sumOfAllAreas } from "./index"; + +describe("exercise4 - problem3", () => { + const circle1 = new Circle(2); + const circle2 = new Circle(3); + const rect1 = new Rectangle(2, 4); + const rect2 = new Rectangle(3, 2); + + test.each([ + { + shapes: [circle1, circle2], + expected: 40, + }, + { + shapes: [rect1, rect2], + expected: 14, + }, + { + shapes: [circle1, rect1], + expected: 20, + }, + { + shapes: [circle1, rect2, circle2], + expected: 46, + }, + { + shapes: [rect1, circle1, rect2, circle2], + expected: 54, + }, + ])( + "Math.floor(sumOfAllAreas($shapes)) -> ~$expected", + ({ shapes, expected }) => { + expect(Math.floor(sumOfAllAreas(...shapes))).toBe(expected); + } + ); +}); diff --git a/exercise4/problem3/index.ts b/exercise4/problem3/index.ts new file mode 100644 index 0000000..377d2ec --- /dev/null +++ b/exercise4/problem3/index.ts @@ -0,0 +1,8 @@ +// Update it as much as you want, just don't change the name +export class Circle {} + +// Update it as much as you want, just don't change the name +export class Rectangle {} + +// Update it as much as you want, just don't change the name +export function sumOfAllAreas() {}