Skip to content
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
25 changes: 24 additions & 1 deletion Geometry/Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@
*/
export default class Circle {
constructor(radius) {
if (radius <= 0) {
throw new RangeError('Radius should be a positive number')
}
this.radius = radius
}

// Formular to Calculate the perimeter of a circle
perimeter = () => {
return this.radius * 2 * Math.PI
}

// Formular to Calculate the area of a circle
area = () => {
return Math.pow(this.radius, 2) * Math.PI
}

// Formular to Calculate the diameter of a circle
diameter = () => {
return 2 * this.radius
}
// Function That Changes the value of the default radius
changeRadius = (newRadius) => {
if (newRadius <= 0) {
throw new RangeError('New radius should be a positive number')
}
this.radius = newRadius
}
// Function That Calculates the length of an arc
lengthOfArc = (angle) => {
if (angle <= 0) {
throw new RangeError('Angle should be a positive number')
}
return this.radius * angle
}
}
18 changes: 18 additions & 0 deletions Geometry/Test/Circle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@ import Circle from '../Circle'

const circle = new Circle(3)

test('The radius of the circle should be initialized to 3', () => {
expect(circle.radius).toEqual(3)
})

test('The area of a circle with radius equal to 3', () => {
expect(parseFloat(circle.area().toFixed(2))).toEqual(28.27)
})

test('The perimeter of a circle with radius equal to 3', () => {
expect(parseFloat(circle.perimeter().toFixed(2))).toEqual(18.85)
})

test('The diameter of a circle with radius equal to 3', () => {
expect(parseFloat(circle.diameter().toFixed(2))).toEqual(6.0)
})

test('Change radius and check if it reflects in calculations', () => {
circle.changeRadius(4)
expect(circle.radius).toEqual(4)
expect(parseFloat(circle.area().toFixed(2))).toEqual(50.27)
})

test('The Length of arc in radian of the circle', () => {
expect(parseFloat(circle.lengthOfArc(2.1).toFixed(2))).toEqual(8.4)
})
9 changes: 3 additions & 6 deletions Recursive/Factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
*/

const factorial = (n) => {
if (!Number.isInteger(n)) {
throw new RangeError('Not a Whole Number')
}

if (n < 0) {
throw new RangeError('Not a Positive Number')
if (!Number.isInteger(n) || n < 0) {
throw new RangeError('Input should be a non-negative whole number')
}

if (n === 0) {
return 1
}

return n * factorial(n - 1)
}

Expand Down
20 changes: 15 additions & 5 deletions Recursive/test/Factorial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ describe('Factorial', () => {
})

it('Throw Error for Invalid Input', () => {
expect(() => factorial('-')).toThrow('Not a Whole Number')
expect(() => factorial(null)).toThrow('Not a Whole Number')
expect(() => factorial(undefined)).toThrow('Not a Whole Number')
expect(() => factorial(3.142)).toThrow('Not a Whole Number')
expect(() => factorial(-1)).toThrow('Not a Positive Number')
expect(() => factorial('-')).toThrow(
'Input should be a non-negative whole number'
)
expect(() => factorial(null)).toThrow(
'Input should be a non-negative whole number'
)
expect(() => factorial(undefined)).toThrow(
'Input should be a non-negative whole number'
)
expect(() => factorial(3.142)).toThrow(
'Input should be a non-negative whole number'
)
expect(() => factorial(-1)).toThrow(
'Input should be a non-negative whole number'
)
})
})