From a2822e17b6ec606efd6b2ef1bd21692502fcc503 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Wed, 16 Feb 2022 19:47:01 +0600 Subject: [PATCH 1/6] pref: optimize the count vowels algo simplify the algo by using regex and String.prototype.match method, and modified the JS Doc --- String/CountVowels.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/String/CountVowels.js b/String/CountVowels.js index 9772c28f03..71939e3882 100644 --- a/String/CountVowels.js +++ b/String/CountVowels.js @@ -1,25 +1,21 @@ /** * @function countVowels * @description Given a string of words or phrases, count the number of vowels. - * @param {String} url - The input string - * @return {Number} count + * @param {String} str - The input string + * @return {Number} - The number of vowel * @example countVowels("ABCDE") => 2 * @example countVowels("Hello") => 2 */ const countVowels = (str) => { if (typeof str !== 'string') { - throw new TypeError('Input should be a string') + throw new TypeError('Input should be a string'); } - const vowels = new Set(['a', 'e', 'i', 'o', 'u']) - let count = 0 - for (let i = 0; i < str.length; i++) { - const char = str[i].toLowerCase() - if (vowels.has(char)) { - count++ - } - } - return count + + const vowelRegex = /[aeiou]/gi; + const vowelsArray = str.match(vowelRegex) || []; + + return vowelsArray.length; } -export { countVowels } +export { countVowels }; From ae99515ae6e65d89b66c0c815514b48adb3a97a3 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal <57553028+fahimfaisaal@users.noreply.github.com> Date: Thu, 17 Feb 2022 09:19:17 +0600 Subject: [PATCH 2/6] fix: resolve all requests --- String/CountVowels.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/String/CountVowels.js b/String/CountVowels.js index 71939e3882..0623cc2e98 100644 --- a/String/CountVowels.js +++ b/String/CountVowels.js @@ -2,14 +2,14 @@ * @function countVowels * @description Given a string of words or phrases, count the number of vowels. * @param {String} str - The input string - * @return {Number} - The number of vowel + * @return {Number} - The number of vowels * @example countVowels("ABCDE") => 2 * @example countVowels("Hello") => 2 */ const countVowels = (str) => { if (typeof str !== 'string') { - throw new TypeError('Input should be a string'); + throw new TypeError('Input should be a string') } const vowelRegex = /[aeiou]/gi; @@ -18,4 +18,4 @@ const countVowels = (str) => { return vowelsArray.length; } -export { countVowels }; +export { countVowels } From 6f401c9aaf199cf142eab4744d58a2075cb1f032 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Thu, 17 Feb 2022 10:30:28 +0600 Subject: [PATCH 3/6] pref: optimize the algo by regex ignore the useless traverse in best case by the help of regex and String.prototype.replace method --- String/Lower.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/String/Lower.js b/String/Lower.js index 3805889948..adf3a361ef 100644 --- a/String/Lower.js +++ b/String/Lower.js @@ -1,8 +1,8 @@ /** * @function lower * @description Will convert the entire string to lowercase letters. - * @param {String} url - The input URL string - * @return {String} Lowercase string + * @param {String} str - The input string + * @returns {String} Lowercase string * @example lower("HELLO") => hello * @example lower("He_llo") => he_llo */ @@ -12,17 +12,13 @@ const lower = (str) => { throw new TypeError('Invalid Input Type') } - let lowerString = '' + const lowerString = str.replace(/[A-Z]/g, (_, indexOfUpperChar) => { + const asciiCode = str.charCodeAt(indexOfUpperChar); - for (const char of str) { - let asciiCode = char.charCodeAt(0) - if (asciiCode >= 65 && asciiCode <= 90) { - asciiCode += 32 - } - lowerString += String.fromCharCode(asciiCode) - } + return String.fromCharCode(asciiCode + 32); + }) - return lowerString + return lowerString; } export { lower } From 4609833da146beafe839682d7558edf9f64c96fc Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Thu, 17 Feb 2022 10:31:34 +0600 Subject: [PATCH 4/6] test: add four new test cases --- String/test/Lower.test.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/String/test/Lower.test.js b/String/test/Lower.test.js index 0fbaaa5d62..527a8a07d4 100644 --- a/String/test/Lower.test.js +++ b/String/test/Lower.test.js @@ -1,9 +1,19 @@ import { lower } from '../Lower' -describe('Lower', () => { - it('return uppercase strings', () => { - expect(lower('hello')).toBe('hello') - expect(lower('WORLD')).toBe('world') - expect(lower('hello_WORLD')).toBe('hello_world') +describe('Testing the Lower function', () => { + it('Test 1: Check by invalid type', () => { + expect(() => lower(345)).toThrowError(); + expect(() => lower(true)).toThrowError(); + expect(() => lower(null)).toThrowError(); + }) + + it('Test 2: Check by uppercase string', () => { + expect(lower('WORLD')).toBe('world'); + expect(lower('Hello_WORLD')).toBe('hello_world'); + }) + + it('Test 3: Check by lowercase string', () => { + expect(lower('hello')).toBe('hello'); + expect(lower('hello_world')).toBe('hello_world'); }) }) From 06b7e3e6c3d835461971b5871d02a54d94ab1fc2 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Thu, 17 Feb 2022 13:01:25 +0600 Subject: [PATCH 5/6] Revert "test: add four new test cases" This reverts commit 4609833da146beafe839682d7558edf9f64c96fc. --- String/Lower.js | 18 +++++++++++------- String/test/Lower.test.js | 20 +++++--------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/String/Lower.js b/String/Lower.js index adf3a361ef..3805889948 100644 --- a/String/Lower.js +++ b/String/Lower.js @@ -1,8 +1,8 @@ /** * @function lower * @description Will convert the entire string to lowercase letters. - * @param {String} str - The input string - * @returns {String} Lowercase string + * @param {String} url - The input URL string + * @return {String} Lowercase string * @example lower("HELLO") => hello * @example lower("He_llo") => he_llo */ @@ -12,13 +12,17 @@ const lower = (str) => { throw new TypeError('Invalid Input Type') } - const lowerString = str.replace(/[A-Z]/g, (_, indexOfUpperChar) => { - const asciiCode = str.charCodeAt(indexOfUpperChar); + let lowerString = '' - return String.fromCharCode(asciiCode + 32); - }) + for (const char of str) { + let asciiCode = char.charCodeAt(0) + if (asciiCode >= 65 && asciiCode <= 90) { + asciiCode += 32 + } + lowerString += String.fromCharCode(asciiCode) + } - return lowerString; + return lowerString } export { lower } diff --git a/String/test/Lower.test.js b/String/test/Lower.test.js index 527a8a07d4..0fbaaa5d62 100644 --- a/String/test/Lower.test.js +++ b/String/test/Lower.test.js @@ -1,19 +1,9 @@ import { lower } from '../Lower' -describe('Testing the Lower function', () => { - it('Test 1: Check by invalid type', () => { - expect(() => lower(345)).toThrowError(); - expect(() => lower(true)).toThrowError(); - expect(() => lower(null)).toThrowError(); - }) - - it('Test 2: Check by uppercase string', () => { - expect(lower('WORLD')).toBe('world'); - expect(lower('Hello_WORLD')).toBe('hello_world'); - }) - - it('Test 3: Check by lowercase string', () => { - expect(lower('hello')).toBe('hello'); - expect(lower('hello_world')).toBe('hello_world'); +describe('Lower', () => { + it('return uppercase strings', () => { + expect(lower('hello')).toBe('hello') + expect(lower('WORLD')).toBe('world') + expect(lower('hello_WORLD')).toBe('hello_world') }) }) From 14eec5db0eefaa4687dc871a357ed516fd74a8e7 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Thu, 17 Feb 2022 17:24:47 +0600 Subject: [PATCH 6/6] style: fromat with standard js --- String/CountVowels.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/String/CountVowels.js b/String/CountVowels.js index 0623cc2e98..d9dc42368a 100644 --- a/String/CountVowels.js +++ b/String/CountVowels.js @@ -12,10 +12,10 @@ const countVowels = (str) => { throw new TypeError('Input should be a string') } - const vowelRegex = /[aeiou]/gi; - const vowelsArray = str.match(vowelRegex) || []; + const vowelRegex = /[aeiou]/gi + const vowelsArray = str.match(vowelRegex) || [] - return vowelsArray.length; + return vowelsArray.length } export { countVowels }