Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb748ae

Browse files
merge: Fixed wordOccurrence algorithm (TheAlgorithms#909)
* resolved: upgrade the algoritm * docs: add js doc
1 parent 378d4ab commit eb748ae

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

‎String/CheckWordOccurrence.js‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
/**
2-
* Check and count occurrence of each word in a string
3-
* Inputs a String eg. Madonna and Boolean
4-
**/
5-
2+
* @function checkWordOccurrence
3+
* @description - this function count all the words in a sentence and return an word occurrence object
4+
* @param {string} str
5+
* @param {boolean} isCaseSensitive
6+
* @returns {Object}
7+
*/
68
const checkWordOccurrence = (str, isCaseSensitive = false) => {
79
if (typeof str !== 'string') {
810
throw new TypeError('The first param should be a string')
911
}
12+
1013
if (typeof isCaseSensitive !== 'boolean') {
1114
throw new TypeError('The second param should be a boolean')
1215
}
1316

14-
const 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-
}
17+
const modifiedStr = isCaseSensitive ? str.toLowerCase() : str
2218

23-
return result
19+
return modifiedStr
20+
.split(/\s+/) // remove all spaces and distribute all word in List
21+
.reduce(
22+
(occurrence, word) => {
23+
occurrence[word] = occurrence[word] + 1 || 1
24+
return occurrence
25+
},
26+
{}
27+
)
2428
}
29+
2530
export { checkWordOccurrence }
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
import { checkWordOccurrence } from '../CheckWordOccurrence'
2-
describe('checkWordOccurrence', () => {
2+
3+
describe('Testing checkWordOccurrence', () => {
34
it('expects throw on insert wrong string', () => {
45
const value = 123
6+
57
expect(() => checkWordOccurrence(value)).toThrow()
68
})
9+
710
it('expect throw on insert wrong param for case sensitive', () => {
811
const value = 'hello'
12+
913
expect(() => checkWordOccurrence(value, value)).toThrow()
1014
})
15+
1116
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-
})
17+
const stringToTest = 'The quick brown fox jumps over the lazy dog'
18+
const expectResult = { The: 1, quick: 1, brown: 1, fox: 1, jumps: 1, over: 1, the: 1, lazy: 1, dog: 1 }
19+
20+
expect(checkWordOccurrence(stringToTest)).toEqual(expectResult)
2121
})
22+
2223
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-
})
24+
const stringToTest = 'The quick brown fox jumps over the lazy dog'
25+
const expectResult = { the: 2, quick: 1, brown: 1, fox: 1, jumps: 1, over: 1, lazy: 1, dog: 1 }
26+
27+
expect(checkWordOccurrence(stringToTest, true)).toEqual(expectResult)
3228
})
3329
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /