From fba34e6ea7d28eb59f1d0cc93634020d3d3e5db1 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 12:59:11 +0530 Subject: [PATCH 01/14] Create RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Recursive/RecursiveLinearSearch.js diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js new file mode 100644 index 0000000000..db67399026 --- /dev/null +++ b/Recursive/RecursiveLinearSearch.js @@ -0,0 +1,27 @@ +/** + * Recursive Linear Search + * + * This function searches for a key within an array using a recursive approach. + * + * @param {Array} arr - The array to search within. + * @param {*} key - The element to search for. + * @param {number} index - (Optional) The current index being checked in the array (default is 0). + * @returns {number} - The index of the element if found, or -1 if not found. + */ +function recursiveLinearSearch(arr, key, index = 0) { + // Base case: If we have searched the entire array and haven't found the key, return -1. + if (index === arr.length) { + return -1; + } + + // Base case: If the current element matches the key, return its index. + if (arr[index] === key) { + return index; + } + + // Recursive case: Continue searching in the rest of the array. + return recursiveLinearSearch(arr, key, index + 1); +} + +export { recursiveLinearSearch }; + From c865c35efd24b3332ddda02bdf1ed8dedc1f672f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:01:02 +0530 Subject: [PATCH 02/14] Create RecursiveLinearSearch.test.js --- Recursive/test/RecursiveLinearSearch.test.js | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Recursive/test/RecursiveLinearSearch.test.js diff --git a/Recursive/test/RecursiveLinearSearch.test.js b/Recursive/test/RecursiveLinearSearch.test.js new file mode 100644 index 0000000000..b30e2314ba --- /dev/null +++ b/Recursive/test/RecursiveLinearSearch.test.js @@ -0,0 +1,30 @@ +import { recursiveLinearSearch } from '../RecursiveLinearSearch'; + +describe('RecursiveLinearSearch', () => { + const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999]; + + it('should return index 3 for searchValue 10', () => { + const searchValue = 10; + expect(recursiveLinearSearch(arr, searchValue)).toBe(3); + }); + + it('should return index 0 for searchValue 2', () => { + const searchValue = 2; + expect(recursiveLinearSearch(arr, searchValue)).toBe(0); + }); + + it('should return index 13 for searchValue 999', () => { + const searchValue = 999; + expect(recursiveLinearSearch(arr, searchValue)).toBe(13); + }); + + it('should return -1 for searchValue 1', () => { + const searchValue = 1; + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); + }); + + it('should return -1 for searchValue 1000', () => { + const searchValue = 1000; + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); + }); +}); From 00c13afd6bfc81655f7d28ba38f98780f9420f1a Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:07:02 +0530 Subject: [PATCH 03/14] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index db67399026..556fdc2f8c 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -1,3 +1,5 @@ +// Ecplanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ + /** * Recursive Linear Search * From 3185e53d6b97edda59bb293950fefb8dbb9c2e0d Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:12:30 +0530 Subject: [PATCH 04/14] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 556fdc2f8c..473dd49e81 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -10,20 +10,19 @@ * @param {number} index - (Optional) The current index being checked in the array (default is 0). * @returns {number} - The index of the element if found, or -1 if not found. */ -function recursiveLinearSearch(arr, key, index = 0) { +function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1; + return -1;; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index; + return index;; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1); + return recursiveLinearSearch(arr, key, index + 1);; } -export { recursiveLinearSearch }; - +export { recursiveLinearSearch };; From 8e77a5e9c3f349b626a5499d7d36cc0060317286 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:15:07 +0530 Subject: [PATCH 05/14] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 473dd49e81..3eb20df2c5 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1;; + return -1 ;; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index;; + return index ;; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1);; + return recursiveLinearSearch(arr, key, index + 1) ;; } -export { recursiveLinearSearch };; +export { recursiveLinearSearch } ;; From a6dd54f09c212719f022a391e9abd2fe61ef8269 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:17:00 +0530 Subject: [PATCH 06/14] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 3eb20df2c5..e0045b11f9 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -1,4 +1,4 @@ -// Ecplanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ +// Explanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ /** * Recursive Linear Search @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1 ;; + return -1; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index ;; + return index; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1) ;; + return recursiveLinearSearch(arr, key, index + 1); } -export { recursiveLinearSearch } ;; +export { recursiveLinearSearch }; From c1961f1814bb6e33cccfc8a14914cdf65c58e59f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:19:44 +0530 Subject: [PATCH 07/14] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index e0045b11f9..9b02132527 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1; + return -1 } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index; + return index } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1); + return recursiveLinearSearch(arr, key, index + 1) } -export { recursiveLinearSearch }; +export { recursiveLinearSearch } From a8391840ca61c31ef7c7b38bbe1eb591b673ca24 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:21:32 +0530 Subject: [PATCH 08/14] Update RecursiveLinearSearch.test.js --- Recursive/test/RecursiveLinearSearch.test.js | 36 ++++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Recursive/test/RecursiveLinearSearch.test.js b/Recursive/test/RecursiveLinearSearch.test.js index b30e2314ba..2a7daa25a3 100644 --- a/Recursive/test/RecursiveLinearSearch.test.js +++ b/Recursive/test/RecursiveLinearSearch.test.js @@ -1,30 +1,30 @@ -import { recursiveLinearSearch } from '../RecursiveLinearSearch'; +import { recursiveLinearSearch } from '../RecursiveLinearSearch' describe('RecursiveLinearSearch', () => { - const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999]; + const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999] it('should return index 3 for searchValue 10', () => { - const searchValue = 10; - expect(recursiveLinearSearch(arr, searchValue)).toBe(3); - }); + const searchValue = 10 + expect(recursiveLinearSearch(arr, searchValue)).toBe(3) + }) it('should return index 0 for searchValue 2', () => { - const searchValue = 2; - expect(recursiveLinearSearch(arr, searchValue)).toBe(0); - }); + const searchValue = 2 + expect(recursiveLinearSearch(arr, searchValue)).toBe(0) + }) it('should return index 13 for searchValue 999', () => { - const searchValue = 999; - expect(recursiveLinearSearch(arr, searchValue)).toBe(13); - }); + const searchValue = 999 + expect(recursiveLinearSearch(arr, searchValue)).toBe(13) + }) it('should return -1 for searchValue 1', () => { - const searchValue = 1; - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); - }); + const searchValue = 1 + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) + }) it('should return -1 for searchValue 1000', () => { - const searchValue = 1000; - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); - }); -}); + const searchValue = 1000 + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) + }) +}) From c1b4f0c019a70080792cc7c305951fcd870e15f2 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:11:15 +0530 Subject: [PATCH 09/14] Delete Recursive/RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 Recursive/RecursiveLinearSearch.js diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js deleted file mode 100644 index 9b02132527..0000000000 --- a/Recursive/RecursiveLinearSearch.js +++ /dev/null @@ -1,28 +0,0 @@ -// Explanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ - -/** - * Recursive Linear Search - * - * This function searches for a key within an array using a recursive approach. - * - * @param {Array} arr - The array to search within. - * @param {*} key - The element to search for. - * @param {number} index - (Optional) The current index being checked in the array (default is 0). - * @returns {number} - The index of the element if found, or -1 if not found. - */ -function recursiveLinearSearch (arr, key, index = 0) { - // Base case: If we have searched the entire array and haven't found the key, return -1. - if (index === arr.length) { - return -1 - } - - // Base case: If the current element matches the key, return its index. - if (arr[index] === key) { - return index - } - - // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1) -} - -export { recursiveLinearSearch } From dd8cb8fcef2734ea0e43119d201376d50fe3a94f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:11:29 +0530 Subject: [PATCH 10/14] Delete Recursive/test/RecursiveLinearSearch.test.js --- Recursive/test/RecursiveLinearSearch.test.js | 30 -------------------- 1 file changed, 30 deletions(-) delete mode 100644 Recursive/test/RecursiveLinearSearch.test.js diff --git a/Recursive/test/RecursiveLinearSearch.test.js b/Recursive/test/RecursiveLinearSearch.test.js deleted file mode 100644 index 2a7daa25a3..0000000000 --- a/Recursive/test/RecursiveLinearSearch.test.js +++ /dev/null @@ -1,30 +0,0 @@ -import { recursiveLinearSearch } from '../RecursiveLinearSearch' - -describe('RecursiveLinearSearch', () => { - const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999] - - it('should return index 3 for searchValue 10', () => { - const searchValue = 10 - expect(recursiveLinearSearch(arr, searchValue)).toBe(3) - }) - - it('should return index 0 for searchValue 2', () => { - const searchValue = 2 - expect(recursiveLinearSearch(arr, searchValue)).toBe(0) - }) - - it('should return index 13 for searchValue 999', () => { - const searchValue = 999 - expect(recursiveLinearSearch(arr, searchValue)).toBe(13) - }) - - it('should return -1 for searchValue 1', () => { - const searchValue = 1 - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) - }) - - it('should return -1 for searchValue 1000', () => { - const searchValue = 1000 - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) - }) -}) From aaef3a8e29ce24b46930119d96cea948bbcc265e Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:25:50 +0530 Subject: [PATCH 11/14] Create Rectangle.js --- Geometry/Rectangle.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Geometry/Rectangle.js diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js new file mode 100644 index 0000000000..ebc1c001b3 --- /dev/null +++ b/Geometry/Rectangle.js @@ -0,0 +1,42 @@ +/** + * Represents a Rectangle with specified length and width. + */ +export default class Rectangle { + /** + * Create a new Rectangle instance. + * + * @param {number} length - The length of the rectangle. + * @param {number} width - The width of the rectangle. + * @throws {Error} If length or width is less than or equal to 0. + */ + constructor(length, width) { + this.length = length + this.width = width + } + + /** + * Calculate the perimeter of the rectangle. + * + * @returns {number} The perimeter of the rectangle. + * @throws {Error} If length or width is less than or equal to 0. + */ + calculatePerimeter() { + if (this.length <= 0 || this.width <= 0) { + throw new Error("Invalid input. Length and width must be greater than 0.") + } + return 2 * (this.length + this.width) + } + + /** + * Calculate the area of the rectangle. + * + * @returns {number} The area of the rectangle. + * @throws {Error} If length or width is less than or equal to 0. + */ + calculateArea() { + if (this.length <= 0 || this.width <= 0) { + throw new Error("Invalid input. Length and width must be greater than 0.") + } + return this.length * this.width + } +} From 82e00cbddee8eed653845ba21dc7a159180d6c31 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:27:04 +0530 Subject: [PATCH 12/14] Update Rectangle.js --- Geometry/Rectangle.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js index ebc1c001b3..cc51d9595c 100644 --- a/Geometry/Rectangle.js +++ b/Geometry/Rectangle.js @@ -1,5 +1,6 @@ /** * Represents a Rectangle with specified length and width. + * Explanation:- https://en.wikipedia.org/wiki/Rectangle */ export default class Rectangle { /** From 2109aa6fe2f670d25d40a0ec9621a481a185c413 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:27:55 +0530 Subject: [PATCH 13/14] Create Rectangle.test.js --- Geometry/Test/Rectangle.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Geometry/Test/Rectangle.test.js diff --git a/Geometry/Test/Rectangle.test.js b/Geometry/Test/Rectangle.test.js new file mode 100644 index 0000000000..4139e4d708 --- /dev/null +++ b/Geometry/Test/Rectangle.test.js @@ -0,0 +1,15 @@ +import Rectangle from '../Rectangle' + +const rectangle = new Rectangle(5, 4) + +test('Calculate the area of a rectangle', () => { + expect(parseFloat(rectangle.calculateArea().toFixed(2))).toEqual(20.00) +}) + +test('Calculate the perimeter of a rectangle', () => { + expect(parseFloat(rectangle.calculatePerimeter().toFixed(2))).toEqual(18.00) +}) + +test('Invalid input for rectangle', () => { + expect(() => new Rectangle(0, 4)).toThrowError('Invalid input. Length and width must be greater than 0.') +}) From 72765bd97f3e810144277a1a2ff24e2fa63ac615 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:33:11 +0530 Subject: [PATCH 14/14] Update Rectangle.js --- Geometry/Rectangle.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js index cc51d9595c..0461036816 100644 --- a/Geometry/Rectangle.js +++ b/Geometry/Rectangle.js @@ -10,7 +10,7 @@ export default class Rectangle { * @param {number} width - The width of the rectangle. * @throws {Error} If length or width is less than or equal to 0. */ - constructor(length, width) { + constructor (length, width) { this.length = length this.width = width } @@ -21,9 +21,9 @@ export default class Rectangle { * @returns {number} The perimeter of the rectangle. * @throws {Error} If length or width is less than or equal to 0. */ - calculatePerimeter() { + calculatePerimeter () { if (this.length <= 0 || this.width <= 0) { - throw new Error("Invalid input. Length and width must be greater than 0.") + throw new Error('Invalid input. Length and width must be greater than 0.') } return 2 * (this.length + this.width) } @@ -34,9 +34,9 @@ export default class Rectangle { * @returns {number} The area of the rectangle. * @throws {Error} If length or width is less than or equal to 0. */ - calculateArea() { + calculateArea () { if (this.length <= 0 || this.width <= 0) { - throw new Error("Invalid input. Length and width must be greater than 0.") + throw new Error('Invalid input. Length and width must be greater than 0.') } return this.length * this.width }