Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Geometry/Rectangle.js
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
Copy link
Collaborator

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"

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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use Math.pow when JS has ** for exponentiation?

}

isSquare = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't isSquare = () => this.length === this.width work?

return this.length === this.width
}
}
19 changes: 19 additions & 0 deletions Geometry/Test/Rectangle.test.js
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', () => {
Copy link
Collaborator

@appgurueu appgurueu Jul 21, 2023

Choose a reason for hiding this comment

The 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)
})