diff --git a/Linear-Algebra/test/test.js b/Linear-Algebra/test/test.js index 5f1892db8..18cd00500 100644 --- a/Linear-Algebra/test/test.js +++ b/Linear-Algebra/test/test.js @@ -11,7 +11,7 @@ var assert = require('assert') var fs = require('fs') // file is included here -eval(fs.readFileSync('src/la_lib.js') + '') +eval(fs.readFileSync('../src/la_lib.js') + '') // Tests goes here // creating some vectors diff --git a/String/CheckWordOccurrence.js b/String/CheckWordOccurrence.js new file mode 100644 index 000000000..d17b2a0cf --- /dev/null +++ b/String/CheckWordOccurrence.js @@ -0,0 +1,25 @@ +/** + * Check and count occurrence of each word in a string + * Inputs a String eg. Madonna and Boolean + */ + +const checkWordOccurrence = (str, isCaseSensitive = false) => { + if (typeof str != 'string') { + throw new TypeError('The first param should be a string'); + } + if (typeof isCaseSensitive != 'boolean') { + throw new TypeError('The second param should be a boolean') + } + + let result = {} + if (str.length > 0) { + for (let i = 0; i < str.length; i++) { + const word = isCaseSensitive ? str[i] : str[i].toUpperCase() + if(/\s/.test(word)) continue; + result[word] = (!result[word]) ? 1 : result[word] + 1 + } + + } + return result; +} +export { checkWordOccurrence } \ No newline at end of file diff --git a/String/CheckWordOcurrence.test.js b/String/CheckWordOcurrence.test.js new file mode 100644 index 000000000..0e57ad655 --- /dev/null +++ b/String/CheckWordOcurrence.test.js @@ -0,0 +1,34 @@ +import {checkWordOccurrence} from './CheckWordOccurrence'; +describe('checkWordOccurrence', () => { + it('expects throw on insert wrong string', () => { + const value = 123; + expect(() => checkWordOccurrence(value)).toThrow(); + }); + it('expect throw on insert wrong param for case sensitive', () => { + const value = 'hello'; + expect(() => checkWordOccurrence(value, value)).toThrow(); + }); + it('check occurrence with case sensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, true); + const expectResult = {A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1}; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); + }); + }); + it('check occurrence with case insensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, false); + const expectResult = {A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1}; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); + }); + + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index eea349d1b..450a58349 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "node-fetch": "2.6.1" }, "standard": { - "env": [ "jest" ] + "env": [ + "jest" + ] }, "devDependencies": { "babel-jest": "^26.3.0",