diff --git a/Geometry/Circle.js b/Geometry/Circle.js index 3e3a3e03f4..914cd264b8 100644 --- a/Geometry/Circle.js +++ b/Geometry/Circle.js @@ -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 + } } diff --git a/Geometry/Test/Circle.test.js b/Geometry/Test/Circle.test.js index b2332df43d..ecd4da4ec4 100644 --- a/Geometry/Test/Circle.test.js +++ b/Geometry/Test/Circle.test.js @@ -2,6 +2,10 @@ 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) }) @@ -9,3 +13,17 @@ test('The area of a circle with radius equal to 3', () => { 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) +}) diff --git a/Recursive/Factorial.js b/Recursive/Factorial.js index 5a0d560514..4e2b3c1bff 100644 --- a/Recursive/Factorial.js +++ b/Recursive/Factorial.js @@ -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) } diff --git a/Recursive/test/Factorial.test.js b/Recursive/test/Factorial.test.js index 5f32a44332..e89be9831e 100644 --- a/Recursive/test/Factorial.test.js +++ b/Recursive/test/Factorial.test.js @@ -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' + ) }) })