-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.js
More file actions
36 lines (28 loc) · 704 Bytes
/
Copy pathtest2.js
File metadata and controls
36 lines (28 loc) · 704 Bytes
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
27
28
29
30
31
32
33
34
35
36
function countWords(string) {
const wordCount = {};
if (string === "") {
return wordCount;
} else {
// write code here
string.split(" ").forEach((word) => {
console.log(word)
const numberOfOccurrences = string.match(new RegExp(word, "g")).length
wordCount[word] = numberOfOccurrences
})}
return wordCount;
}
//console.log(myTextObject)
let string1 = "you're braver than you believe, stronger than you seem, and smarter than you think";
console.log(countWords(string1))
// {
// "you're": 1,
// and: 1,
// "believe,": 1,
// braver: 1,
// "seem,": 1,
// smarter: 1,
// stronger: 1,
// than: 3,
// think: 1,
// you: 3,
// }