Skip to content

Commit

Permalink
update exercise4
Browse files Browse the repository at this point in the history
  • Loading branch information
talgat-ruby committed Oct 17, 2023
1 parent 107157a commit fb37315
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 0 deletions.
14 changes: 14 additions & 0 deletions exercise4/problem1/README.md
Original file line number Diff line number Diff line change
@@ -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."
```
37 changes: 37 additions & 0 deletions exercise4/problem1/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 3 additions & 0 deletions exercise4/problem1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Person {}

export default Person;
19 changes: 19 additions & 0 deletions exercise4/problem2/README.md
Original file line number Diff line number Diff line change
@@ -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"
```
33 changes: 33 additions & 0 deletions exercise4/problem2/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 3 additions & 0 deletions exercise4/problem2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Name {}

export default Name;
13 changes: 13 additions & 0 deletions exercise4/problem3/README.md
Original file line number Diff line number Diff line change
@@ -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. ...
```
37 changes: 37 additions & 0 deletions exercise4/problem3/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
);
});
8 changes: 8 additions & 0 deletions exercise4/problem3/index.ts
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit fb37315

Please sign in to comment.