From dcf7a112628e22db820225dee700a407f4aad996 Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Tue, 11 Oct 2022 00:15:26 +0200 Subject: [PATCH 1/9] feat: Add countCharacters algorithm --- String/CountCharacters.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 String/CountCharacters.js diff --git a/String/CountCharacters.js b/String/CountCharacters.js new file mode 100644 index 0000000000..65c594a561 --- /dev/null +++ b/String/CountCharacters.js @@ -0,0 +1,37 @@ +/** + * @function countCharacters + * @description Given a string, count the number of each character. + * @param {String} str - The input string + * @return {Object} - Object with characters and number of times + * @example countCharacters("hello") => {h: 1, e: 1, l: 2, o: 1} + */ + +const countCharacters = (str) => { + const specialChars = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/ + + if (typeof str !== 'string') { + throw new TypeError('Input should be a string') + } + + if (specialChars.test(str)) { + throw new TypeError('Input must not contain special characters') + } + + if (/\d/.test(str)) { + throw new TypeError('Input must not contain numbers') + } + + var obj = {} + for (var i = 0; i < str.toLowerCase().length; i++) { + var char = str.toLowerCase().charAt(i) + if (obj[char]) { + obj[char]++ + } else { + obj[char] = 1 + } + } + + return obj +} + +export { countCharacters } From 92e20be2911fe71be3aac79bef0bf802d7097011 Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Tue, 11 Oct 2022 00:15:48 +0200 Subject: [PATCH 2/9] tests: Add countCharacters tests --- String/test/CountCharacters.test.js | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 String/test/CountCharacters.test.js diff --git a/String/test/CountCharacters.test.js b/String/test/CountCharacters.test.js new file mode 100644 index 0000000000..0ad0b44bfd --- /dev/null +++ b/String/test/CountCharacters.test.js @@ -0,0 +1,33 @@ +import { countCharacters } from '../CountCharacters' + +describe('CountVowels', () => { + it('expect throws on use wrong param', () => { + expect(() => countCharacters(0)).toThrow() + }) + + it('expect throws when using a number in the string', () => { + expect(() => countCharacters('h3llo')).toThrow() + }) + + it('expect throws when using a special characters in the string', () => { + expect(() => countCharacters('hello!')).toThrow() + }) + + it('count the characters in a string. Allows lower case', () => { + const value = 'hello' + const count = countCharacters(value) + expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 }) + }) + + it('count the characters in a string. Allows upper case', () => { + const value = 'HELLO' + const count = countCharacters(value) + expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 }) + }) + + it('count the characters in a string. Allows upper and lower case', () => { + const value = 'HelLo' + const count = countCharacters(value) + expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 }) + }) +}) From 0ecc0d78ab8482bedec6f503b84fc521533dfe87 Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Tue, 11 Oct 2022 00:19:47 +0200 Subject: [PATCH 3/9] change var to let --- String/CountCharacters.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/String/CountCharacters.js b/String/CountCharacters.js index 65c594a561..9a17c52b7e 100644 --- a/String/CountCharacters.js +++ b/String/CountCharacters.js @@ -21,9 +21,9 @@ const countCharacters = (str) => { throw new TypeError('Input must not contain numbers') } - var obj = {} - for (var i = 0; i < str.toLowerCase().length; i++) { - var char = str.toLowerCase().charAt(i) + let obj = {} + for (let i = 0; i < str.toLowerCase().length; i++) { + let char = str.toLowerCase().charAt(i) if (obj[char]) { obj[char]++ } else { From 48961e5b1395b0f8bde13221fbf253b23ad576fc Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Tue, 11 Oct 2022 00:28:53 +0200 Subject: [PATCH 4/9] change let to const --- String/CountCharacters.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/String/CountCharacters.js b/String/CountCharacters.js index 9a17c52b7e..2e9e316d9c 100644 --- a/String/CountCharacters.js +++ b/String/CountCharacters.js @@ -21,9 +21,9 @@ const countCharacters = (str) => { throw new TypeError('Input must not contain numbers') } - let obj = {} + const obj = {} for (let i = 0; i < str.toLowerCase().length; i++) { - let char = str.toLowerCase().charAt(i) + const char = str.toLowerCase().charAt(i) if (obj[char]) { obj[char]++ } else { From 664b0da1f6b929194e8129c36c49040248e93ef2 Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Tue, 11 Oct 2022 00:34:30 +0200 Subject: [PATCH 5/9] fix: regEx --- String/CountCharacters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/CountCharacters.js b/String/CountCharacters.js index 2e9e316d9c..461018de97 100644 --- a/String/CountCharacters.js +++ b/String/CountCharacters.js @@ -7,7 +7,7 @@ */ const countCharacters = (str) => { - const specialChars = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/ + const specialChars = /[`!@#$%^&*()_+\-=[\]{};':"\\|,.<>?~]/ if (typeof str !== 'string') { throw new TypeError('Input should be a string') From cf56c3c71ec4c5d4bcebcea09d93e60b3690488b Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Tue, 11 Oct 2022 08:25:52 +0200 Subject: [PATCH 6/9] fix: change line --- String/CountCharacters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/CountCharacters.js b/String/CountCharacters.js index 461018de97..eeb8daa205 100644 --- a/String/CountCharacters.js +++ b/String/CountCharacters.js @@ -25,7 +25,7 @@ const countCharacters = (str) => { for (let i = 0; i < str.toLowerCase().length; i++) { const char = str.toLowerCase().charAt(i) if (obj[char]) { - obj[char]++ + obj[char] = (obj[char] || 0) + 1 } else { obj[char] = 1 } From 25891c62703831f54dd7aeffc9e1397a9c0c0412 Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Tue, 11 Oct 2022 11:24:35 +0200 Subject: [PATCH 7/9] replace if-else condition --- String/CountCharacters.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/String/CountCharacters.js b/String/CountCharacters.js index eeb8daa205..b13c19d3f7 100644 --- a/String/CountCharacters.js +++ b/String/CountCharacters.js @@ -24,11 +24,7 @@ const countCharacters = (str) => { const obj = {} for (let i = 0; i < str.toLowerCase().length; i++) { const char = str.toLowerCase().charAt(i) - if (obj[char]) { - obj[char] = (obj[char] || 0) + 1 - } else { - obj[char] = 1 - } + obj[char] = (obj[char] || 0) + 1 } return obj From de7033295742c0bc818825eba913b7a62e712bcd Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Mon, 17 Oct 2022 21:08:22 +0200 Subject: [PATCH 8/9] refactor: Change name to CountLetters --- String/{CountCharacters.js => CountLetters.js} | 6 +++--- ...ntCharacters.test.js => CountLetters.test.js} | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) rename String/{CountCharacters.js => CountLetters.js} (85%) rename String/test/{CountCharacters.test.js => CountLetters.test.js} (65%) diff --git a/String/CountCharacters.js b/String/CountLetters.js similarity index 85% rename from String/CountCharacters.js rename to String/CountLetters.js index b13c19d3f7..8788c45746 100644 --- a/String/CountCharacters.js +++ b/String/CountLetters.js @@ -6,8 +6,8 @@ * @example countCharacters("hello") => {h: 1, e: 1, l: 2, o: 1} */ -const countCharacters = (str) => { - const specialChars = /[`!@#$%^&*()_+\-=[\]{};':"\\|,.<>?~]/ +const countLetters = (str) => { + const specialChars = /\W/g if (typeof str !== 'string') { throw new TypeError('Input should be a string') @@ -30,4 +30,4 @@ const countCharacters = (str) => { return obj } -export { countCharacters } +export { countLetters } diff --git a/String/test/CountCharacters.test.js b/String/test/CountLetters.test.js similarity index 65% rename from String/test/CountCharacters.test.js rename to String/test/CountLetters.test.js index 0ad0b44bfd..7ff2b4b743 100644 --- a/String/test/CountCharacters.test.js +++ b/String/test/CountLetters.test.js @@ -1,33 +1,33 @@ -import { countCharacters } from '../CountCharacters' +import { countLetters } from '../CountLetters' -describe('CountVowels', () => { +describe('CountLetters', () => { it('expect throws on use wrong param', () => { - expect(() => countCharacters(0)).toThrow() + expect(() => countLetters(0)).toThrow() }) it('expect throws when using a number in the string', () => { - expect(() => countCharacters('h3llo')).toThrow() + expect(() => countLetters('h3llo')).toThrow() }) it('expect throws when using a special characters in the string', () => { - expect(() => countCharacters('hello!')).toThrow() + expect(() => countLetters('hello!')).toThrow() }) it('count the characters in a string. Allows lower case', () => { const value = 'hello' - const count = countCharacters(value) + const count = countLetters(value) expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 }) }) it('count the characters in a string. Allows upper case', () => { const value = 'HELLO' - const count = countCharacters(value) + const count = countLetters(value) expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 }) }) it('count the characters in a string. Allows upper and lower case', () => { const value = 'HelLo' - const count = countCharacters(value) + const count = countLetters(value) expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 }) }) }) From fe3017b1982aaf32a50c38e7b368bbbcafa088a7 Mon Sep 17 00:00:00 2001 From: Sandra Laguna Date: Mon, 17 Oct 2022 21:13:21 +0200 Subject: [PATCH 9/9] fix: change "characters" for "letters" --- String/CountLetters.js | 8 ++++---- String/test/CountLetters.test.js | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/String/CountLetters.js b/String/CountLetters.js index 8788c45746..6b64001505 100644 --- a/String/CountLetters.js +++ b/String/CountLetters.js @@ -1,9 +1,9 @@ /** - * @function countCharacters - * @description Given a string, count the number of each character. + * @function countLetters + * @description Given a string, count the number of each letter. * @param {String} str - The input string - * @return {Object} - Object with characters and number of times - * @example countCharacters("hello") => {h: 1, e: 1, l: 2, o: 1} + * @return {Object} - Object with letters and number of times + * @example countLetters("hello") => {h: 1, e: 1, l: 2, o: 1} */ const countLetters = (str) => { diff --git a/String/test/CountLetters.test.js b/String/test/CountLetters.test.js index 7ff2b4b743..424dcfa31d 100644 --- a/String/test/CountLetters.test.js +++ b/String/test/CountLetters.test.js @@ -13,19 +13,19 @@ describe('CountLetters', () => { expect(() => countLetters('hello!')).toThrow() }) - it('count the characters in a string. Allows lower case', () => { + it('count the letters in a string. Allows lower case', () => { const value = 'hello' const count = countLetters(value) expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 }) }) - it('count the characters in a string. Allows upper case', () => { + it('count the letters in a string. Allows upper case', () => { const value = 'HELLO' const count = countLetters(value) expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 }) }) - it('count the characters in a string. Allows upper and lower case', () => { + it('count the letters in a string. Allows upper and lower case', () => { const value = 'HelLo' const count = countLetters(value) expect(count).toEqual({ h: 1, e: 1, l: 2, o: 1 })