-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
algorithm class: rectangle #1340
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* This class represents a rectangle and can calculate it's perimeter,area,diagonal's length | ||
* and check whether it is square or not. | ||
* https://en.wikipedia.org/wiki/Rectangle | ||
* @constructor | ||
* @param {number} length - The length of the rectangle. | ||
* @param {number} width - The width of the rectangle. | ||
*/ | ||
|
||
export default class Rectangle { | ||
constructor (length, width) { | ||
this.length = length | ||
this.width = width | ||
} | ||
|
||
perimeter = () => { | ||
return 2 * (this.length + this.width) | ||
} | ||
|
||
area = () => { | ||
return this.length * this.width | ||
} | ||
|
||
diagonalLength = () => { | ||
return Math.sqrt(Math.pow(this.length, 2) + Math.pow(this.width, 2)) | ||
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. Why use |
||
} | ||
|
||
isSquare = () => { | ||
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. Won't |
||
return this.length === this.width | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Rectangle from '../Rectangle' | ||
|
||
const rectangle = new Rectangle(4, 2) | ||
|
||
test('The area of a rectangle with length equal to 4 and width equal to 2', () => { | ||
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. These tests are trivial; they don't need redundant names. |
||
expect(parseFloat(rectangle.area().toFixed(2))).toEqual(8.00) | ||
}) | ||
|
||
test('The perimeter of a rectangle with length equal to 4 and width equal to 2', () => { | ||
expect(parseFloat(rectangle.perimeter().toFixed(2))).toEqual(12.00) | ||
}) | ||
|
||
test('The length of each diagonal in a rectangle with length equal to 4 and width equal to 2', () => { | ||
expect(parseFloat(rectangle.diagonalLength().toFixed(2))).toEqual(4.47) | ||
}) | ||
|
||
test('A rectangle with length equal to 4 and width equal to 2 is square ? : ', () => { | ||
expect(rectangle.isSquare()).toEqual(false) | ||
}) |
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.
I think this is usually called "height"