Skip to content

Commit bd70fd2

Browse files
committed
Added new algoritm
1 parent 0dfb200 commit bd70fd2

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

Linear-Algebra/test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var assert = require('assert')
1111
var fs = require('fs')
1212

1313
// file is included here
14-
eval(fs.readFileSync('src/la_lib.js') + '')
14+
eval(fs.readFileSync('../src/la_lib.js') + '')
1515
// Tests goes here
1616

1717
// creating some vectors

String/CheckWordOccurrence.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Check and count occurrence of each word in a string
3+
* Inputs a String eg. Madonna and Boolean
4+
*/
5+
6+
const checkWordOccurrence = (str, isCaseSensitive = false) => {
7+
if (typeof str != 'string') {
8+
throw new TypeError('The first param should be a string');
9+
}
10+
if (typeof isCaseSensitive != 'boolean') {
11+
throw new TypeError('The second param should be a boolean')
12+
}
13+
14+
let result = {}
15+
if (str.length > 0) {
16+
for (let i = 0; i < str.length; i++) {
17+
const word = isCaseSensitive ? str[i] : str[i].toUpperCase()
18+
if(/\s/.test(word)) continue;
19+
result[word] = (!result[word]) ? 1 : result[word] + 1
20+
}
21+
22+
}
23+
return result;
24+
}
25+
export { checkWordOccurrence }

String/CheckWordOcurrence.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {checkWordOccurrence} from './CheckWordOccurrence';
2+
describe('checkWordOccurrence', () => {
3+
it('expects throw on insert wrong string', () => {
4+
const value = 123;
5+
expect(() => checkWordOccurrence(value)).toThrow();
6+
});
7+
it('expect throw on insert wrong param for case sensitive', () => {
8+
const value = 'hello';
9+
expect(() => checkWordOccurrence(value, value)).toThrow();
10+
});
11+
it('check occurrence with case sensitive', () => {
12+
const stringToTest = "A Mad World";
13+
const charsOccurrences = checkWordOccurrence(stringToTest, true);
14+
const expectResult = {A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1};
15+
const occurrencesObjectKeys = Object.keys(charsOccurrences);
16+
const expectObjectKeys = Object.keys(expectResult);
17+
expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length);
18+
expectObjectKeys.forEach(key => {
19+
expect(expectResult[key]).toBe(charsOccurrences[key]);
20+
});
21+
});
22+
it('check occurrence with case insensitive', () => {
23+
const stringToTest = "A Mad World";
24+
const charsOccurrences = checkWordOccurrence(stringToTest, false);
25+
const expectResult = {A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1};
26+
const occurrencesObjectKeys = Object.keys(charsOccurrences);
27+
const expectObjectKeys = Object.keys(expectResult);
28+
expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length);
29+
expectObjectKeys.forEach(key => {
30+
expect(expectResult[key]).toBe(charsOccurrences[key]);
31+
});
32+
33+
});
34+
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"node-fetch": "2.6.1"
1717
},
1818
"standard": {
19-
"env": [ "jest" ]
19+
"env": [
20+
"jest"
21+
]
2022
},
2123
"devDependencies": {
2224
"babel-jest": "^26.3.0",

0 commit comments

Comments
 (0)