Skip to content

Commit 12fb231

Browse files
committed
Complete stretch exercises for Sprint 2
1 parent af702c4 commit 12fb231

2 files changed

Lines changed: 34 additions & 38 deletions

File tree

Sprint-2/stretch/count-words.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
/*
2-
Count the number of times a word appears in a given string.
1+
function countWords(text) {
2+
if (typeof text !== "string") {
3+
throw new Error("Input must be a string");
4+
}
35

4-
Write a function called countWords that
5-
- takes a string as an argument
6-
- returns an object where
7-
- the keys are the words from the string and
8-
- the values are the number of times the word appears in the string
6+
// remove punctuation and make lowercase
7+
const cleanedText = text.toLowerCase().replace(/[.,!?]/g, "");
98

10-
Example
11-
If we call countWords like this:
9+
const words = cleanedText.split(/\s+/);
1210

13-
countWords("you and me and you") then the target output is { you: 2, and: 2, me: 1 }
11+
const counts = {};
1412

15-
To complete this exercise you should understand
16-
- Strings and string manipulation
17-
- Loops
18-
- Comparison inside if statements
19-
- Setting values on an object
13+
for (const word of words) {
14+
if (!word) continue;
2015

21-
## Advanced challenges
16+
if (counts[word]) {
17+
counts[word]++;
18+
} else {
19+
counts[word] = 1;
20+
}
21+
}
2222

23-
1. Remove all of the punctuation (e.g. ".", ",", "!", "?") to tidy up the results
23+
return counts;
24+
}
2425

25-
2. Ignore the case of the words to find more unique words. e.g. (A === a, Hello === hello)
26-
27-
3. Order the results to find out which word is the most common in the input
28-
*/
26+
module.exports = countWords;

Sprint-2/stretch/mode.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
// You are given an implementation of calculateMode
1+
function trackFrequencies(list) {
2+
const freqs = new Map();
23

3-
// calculateMode's implementation can be broken down into two stages:
4-
5-
// Stage 1. One part of the code tracks the frequency of each value
6-
// Stage 2. The other part finds the value with the highest frequency
7-
8-
// refactor calculateMode by splitting up the code
9-
// into smaller functions using the stages above
10-
11-
function calculateMode(list) {
12-
// track frequency of each value
13-
let freqs = new Map();
14-
15-
for (let num of list) {
4+
for (const num of list) {
165
if (typeof num !== "number") {
176
continue;
187
}
198

209
freqs.set(num, (freqs.get(num) || 0) + 1);
2110
}
2211

23-
// Find the value with the highest frequency
12+
return freqs;
13+
}
14+
15+
function findMode(freqs) {
2416
let maxFreq = 0;
2517
let mode;
26-
for (let [num, freq] of freqs) {
18+
19+
for (const [num, freq] of freqs) {
2720
if (freq > maxFreq) {
28-
mode = num;
2921
maxFreq = freq;
22+
mode = num;
3023
}
3124
}
3225

3326
return maxFreq === 0 ? NaN : mode;
3427
}
3528

29+
function calculateMode(list) {
30+
const freqs = trackFrequencies(list);
31+
return findMode(freqs);
32+
}
33+
3634
module.exports = calculateMode;

0 commit comments

Comments
 (0)