forked from denulemos/programming-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ejer3.js
26 lines (19 loc) · 744 Bytes
/
ejer3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const searchWord = (word, phrase) => {
// Limpiamos la frase, la ponemos en minuscula y retiramos las comas
const cleanPhrase = phrase.toLowerCase().replace(',', '');
// Existe la palabra buscada dentro de la frase?
const wordFound = cleanPhrase.includes(word);
if (wordFound){
// Armamos un array con todas las palabras de la frase
const words = cleanPhrase.split(" ");
var map = {};
// Iteramos por palabra y contamos sus ocurrencias
words.forEach(wordIterated => {
map[wordIterated] ? map[wordIterated]++ : map[wordIterated] = 1;
});
const result = map[word];
return result;
}
return 0;
}
module.exports = searchWord;