-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-count-words.js
More file actions
71 lines (57 loc) · 1.71 KB
/
Copy path1-count-words.js
File metadata and controls
71 lines (57 loc) · 1.71 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
This exercise is to write function that counts the number of times each word appears in a string.
Write a function called countWords that
- takes a string as an argument
- returns an object where
- the keys are the words from the string and
- the values are the number of times the word appears in the string
Example
If the function is given the input:
"you and me and you";
the object returned would be:
{ you: 2, and: 2, me: 1 }
To complete this exercise you should understand
- Strings and string manipulation
- Loops or forEach
- Comparison inside if statements
- Setting values on an object
*/
function countWords(string) {
const wordCount = {};
// write code here
return wordCount;
}
/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm run extra-tests`
- To run all exercises/tests in the mandatory folder, run `npm test`
- (Reminder: You must have run `npm install` one time before this will work!)
*/
test("Code works for a small string", () => {
expect(countWords("I love CodeYourFuture")).toEqual({
I: 1,
love: 1,
CodeYourFuture: 1,
});
});
test("A string with, some punctuation", () => {
expect(countWords("A string with, some punctuation")).toEqual(
{ A: 1, string: 1, "with,": 1, some: 1, punctuation: 1 }
);
});
test("Empty string", () => {
expect(countWords("")).toEqual({});
});
test("Example task string", () => {
expect(countWords("you're braver than you believe, stronger than you seem, and smarter than you think")).toEqual({
"you're": 1,
and: 1,
"believe,": 1,
braver: 1,
"seem,": 1,
smarter: 1,
stronger: 1,
than: 3,
think: 1,
you: 3,
});
});