diff --git a/Maths/Factorial.js b/Maths/Factorial.js index 29b416a67e..9b77b8d8f3 100644 --- a/Maths/Factorial.js +++ b/Maths/Factorial.js @@ -19,18 +19,18 @@ const calcRange = (num) => { const calcFactorial = (num) => { if (num === 0) { - return 'The factorial of 0 is 1.' + return 1 } if (num < 0) { - return 'Sorry, factorial does not exist for negative numbers.' + throw Error('Sorry, factorial does not exist for negative numbers.') } if (!num) { - return 'Sorry, factorial does not exist for null or undefined numbers.' + throw Error('Sorry, factorial does not exist for null or undefined numbers.') } if (num > 0) { const range = calcRange(num) const factorial = range.reduce((a, c) => a * c, 1) - return `The factorial of ${num} is ${factorial}` + return factorial } } diff --git a/Maths/test/Factorial.test.js b/Maths/test/Factorial.test.js index a065194774..1f5da42bb6 100644 --- a/Maths/test/Factorial.test.js +++ b/Maths/test/Factorial.test.js @@ -2,30 +2,20 @@ import { calcFactorial } from '../Factorial' describe('calcFactorial', () => { it('should return a statement for value "0"', () => { - expect(calcFactorial(0)).toBe('The factorial of 0 is 1.') + expect(calcFactorial(0)).toBe(1) }) - it('should return a statement for "null" and "undefined"', () => { - const nullFactorial = calcFactorial(null) - const undefinedFactorial = calcFactorial(undefined) - - expect(nullFactorial).toBe( - 'Sorry, factorial does not exist for null or undefined numbers.' - ) - expect(undefinedFactorial).toBe( - 'Sorry, factorial does not exist for null or undefined numbers.' - ) + it('should throw error for "null" and "undefined"', () => { + expect(() => { calcFactorial(null) }).toThrow(Error) + expect(() => { calcFactorial(undefined) }).toThrow(Error) }) - it('should not support negative numbers', () => { - const negativeFactorial = calcFactorial(-5) - expect(negativeFactorial).toBe( - 'Sorry, factorial does not exist for negative numbers.' - ) + it('should throw error for negative numbers', () => { + expect(() => { calcFactorial(-1) }).toThrow(Error) }) it('should return the factorial of a positive number', () => { const positiveFactorial = calcFactorial(3) - expect(positiveFactorial).toBe('The factorial of 3 is 6') + expect(positiveFactorial).toBe(6) }) }) diff --git a/String/CountSubstrings.js b/String/CountSubstrings.js new file mode 100644 index 0000000000..f936f8eab0 --- /dev/null +++ b/String/CountSubstrings.js @@ -0,0 +1,29 @@ +/** + * @function countSubstrings + * @description Given a string of words or phrases, count the occurrences of a substring + * @param {String} str - The input string + * @param {String} substring - The substring + * @return {Number} - The number of substring occurrences + * @example countSubstrings("This is a string", "is") => 2 + * @example countSubstrings("Hello", "e") => 1 + */ + +const countSubstrings = (str, substring) => { + if (typeof str !== 'string' || typeof substring !== 'string') { + throw new TypeError('Argument should be string') + } + + if (substring.length === 0) return str.length + 1 + + let count = 0 + let position = str.indexOf(substring) + + while (position > -1) { + count++ + position = str.indexOf(substring, position + 1) + } + + return count +} + +export { countSubstrings } diff --git a/String/test/CountSubstrings.test.js b/String/test/CountSubstrings.test.js new file mode 100644 index 0000000000..66628b1a14 --- /dev/null +++ b/String/test/CountSubstrings.test.js @@ -0,0 +1,52 @@ +import { countSubstrings } from '../CountSubstrings' + +describe('CountSubstrings', () => { + it('count multiple occurrences of substring in a string', () => { + const str = 'This is a string' + const substring = 'is' + const count = countSubstrings(str, substring) + expect(count).toBe(2) + }) + + it('should return 0 when input substring has no occurrences', () => { + const str = 'Jurassic Park' + const substring = 'World' + const count = countSubstrings(str, substring) + expect(count).toBe(0) + }) + + it('should return 1 when input substring is of length 1 that is equal to string', () => { + const str = 's' + const substring = 's' + const count = countSubstrings(str, substring) + expect(count).toBe(1) + }) + + it('should return the correct result when input string contains spaces', () => { + const str = 'ab cd ef ghi' + const substring = ' ' + const count = countSubstrings(str, substring) + expect(count).toBe(4) + }) + + it('should return the correct result when input substring contains number or special characters', () => { + const str = 'abc1@2def1@2' + const substring = '1@2' + const count = countSubstrings(str, substring) + expect(count).toBe(2) + }) + + it('should return string.length + 1 when the input substring is an empty string', () => { + const str = 'empty' + const substring = '' + const count = countSubstrings(str, substring) + expect(count).toBe(6) + }) + + it('should return correct result when input is overlapping substring', () => { + const str = 'aaa' + const substring = 'aa' + const count = countSubstrings(str, substring) + expect(count).toBe(2) + }) +})