-
Notifications
You must be signed in to change notification settings - Fork 3.1k
solution #3337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
solution #3337
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,80 @@ | ||
| export interface Figure {} | ||
| export type Shape = 'triangle' | 'circle' | 'rectangle'; | ||
| export type Color = 'red' | 'green' | 'blue'; | ||
| export interface Figure { | ||
| shape: Shape; | ||
| color: Color; | ||
| getArea(): number; | ||
| } | ||
|
|
||
| export class Triangle implements Figure { | ||
| readonly shape: Shape = 'triangle'; | ||
|
|
||
| constructor( | ||
| public readonly color: Color, | ||
| private readonly a: number, | ||
| private readonly b: number, | ||
| private readonly c: number, | ||
| ) { | ||
| if (a <= 0 || b <= 0 || c <= 0) { | ||
| throw new Error('The side length is less than or equal to zero.'); | ||
| } | ||
|
|
||
| const maxSide = Math.max(a, b, c); | ||
| const sumLengthTwoSides = a + b + c - maxSide; | ||
|
|
||
| if (maxSide >= sumLengthTwoSides) { | ||
| throw new Error('Length of the longer side: invalid value'); | ||
| } | ||
| } | ||
|
|
||
| getArea(): number { | ||
| const s: number = (this.a + this.b + this.c) / 2; | ||
|
|
||
| return ( | ||
| Math.floor( | ||
| Math.sqrt(s * (s - this.a) * (s - this.b) * (s - this.c)) * 100, | ||
| ) / 100 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export class Circle implements Figure { | ||
| readonly shape: Shape = 'circle'; | ||
|
|
||
| export class Triangle implements Figure {} | ||
| constructor( | ||
| public readonly color: Color, | ||
| private readonly radius: number, | ||
| ) { | ||
| if (this.radius <= 0) { | ||
| throw new Error('The radius is equal to zero.'); | ||
|
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition checks |
||
| } | ||
| } | ||
|
|
||
| getArea(): number { | ||
| return Math.floor(Math.PI * this.radius ** 2 * 100) / 100; | ||
| } | ||
| } | ||
|
|
||
| export class Circle implements Figure {} | ||
| export class Rectangle implements Figure { | ||
| readonly shape: Shape = 'rectangle'; | ||
|
|
||
| constructor( | ||
| public readonly color: Color, | ||
| private readonly width: number, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: 'hight' should be 'height' to match the task description. This typo propagates to lines 65 and 71. |
||
| private readonly hight: number, | ||
| ) { | ||
| if (this.width <= 0 || this.hight <= 0) { | ||
| throw new Error('The side length is less than or equal to zero.'); | ||
| } | ||
| } | ||
|
|
||
| getArea(): number { | ||
| return Math.floor(this.width * this.hight * 100) / 100; | ||
| } | ||
| } | ||
|
|
||
| export class Rectangle implements Figure {} | ||
| export function getInfo(figure: Figure): string { | ||
| const calcArea = figure.getArea(); | ||
|
|
||
| export function getInfo(figure): string { | ||
| return typeof figure; | ||
| return `A ${figure.color} ${figure.shape} - ${calcArea}`; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to checklist item #3, error messages should be detailed. Consider: "The longest side must be less than the sum of the other two sides" instead of the generic "invalid value".