-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
107157a
commit fb37315
Showing
9 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Person {} | ||
|
||
export default Person; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Name {} | ||
|
||
export default Name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. ... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |