-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-count-words.js
More file actions
110 lines (94 loc) · 2.51 KB
/
1-count-words.js
File metadata and controls
110 lines (94 loc) · 2.51 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
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
*/
<<<<<<< HEAD
function countWords(str) {
let array = str.split(" ");
const counts = {};
for (let i = 0; i < array.length; i++) {
const word = array[i];
if (counts[word]) {
counts[word]++;
} else {
counts[word] = 1;
}
}
return counts;
=======
function countWords(string) {
// write code here
let strLower = string.toLowerCase();
const wordCount = {};
// split the string into an array of words
const words = strLower.split(" ");
// loop through each word
words.forEach((word) => {
// check if the word already exists in the object
if (wordCount[word]) {
// if it does, increment the value of that key
wordCount[word]++;
} else {
// if it doesn't, add it to the object with a value of 1
wordCount[word] = 1;
}
});
return wordCount;
>>>>>>> 26cab01f55b1033f71c12f38340eba5bac196f45
}
/* ======= 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,
});
});