Skip to content

Commit d6163d6

Browse files
Complete Sprint 2 stretch exercise
1 parent f1c0798 commit d6163d6

5 files changed

Lines changed: 88 additions & 31 deletions

File tree

‎Sprint-2/stretch/count-words.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,23 @@
2626
2727
3. Order the results to find out which word is the most common in the input
2828
*/
29+
30+
function countWords(text) {
31+
const cleanedText = text
32+
.toLowerCase()
33+
.replace(/[.,!?]/g, "");
34+
35+
const words = cleanedText
36+
.split(" ")
37+
.filter((word) => word !== "");
38+
39+
const wordCounts = {};
40+
41+
for (const word of words) {
42+
wordCounts[word] = (wordCounts[word] || 0) + 1;
43+
}
44+
45+
return wordCounts;
46+
}
47+
48+
module.exports = countWords;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const countWords = require("./count-words.js");
2+
3+
test("counts how many times each word appears", () => {
4+
expect(countWords("you and me and you")).toEqual({
5+
you: 2,
6+
and: 2,
7+
me: 1,
8+
});
9+
});
10+
11+
test("ignores capital letters and punctuation", () => {
12+
expect(countWords("Hello, hello! How are you?")).toEqual({
13+
hello: 2,
14+
how: 1,
15+
are: 1,
16+
you: 1,
17+
});
18+
});
19+
20+
test("returns an empty object for an empty string", () => {
21+
expect(countWords("")).toEqual({});
22+
});

‎Sprint-2/stretch/mode.js‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,37 @@
88
// refactor calculateMode by splitting up the code
99
// into smaller functions using the stages above
1010

11-
function calculateMode(list) {
12-
// track frequency of each value
13-
let freqs = new Map();
11+
function countFrequencies(list) {
12+
const frequencies = new Map();
1413

15-
for (let num of list) {
16-
if (typeof num !== "number") {
14+
for (const number of list) {
15+
if (typeof number !== "number") {
1716
continue;
1817
}
1918

20-
freqs.set(num, (freqs.get(num) || 0) + 1);
19+
frequencies.set(number, (frequencies.get(number) || 0) + 1);
2120
}
2221

23-
// Find the value with the highest frequency
24-
let maxFreq = 0;
22+
return frequencies;
23+
}
24+
25+
function findMostFrequent(frequencies) {
26+
let highestFrequency = 0;
2527
let mode;
26-
for (let [num, freq] of freqs) {
27-
if (freq > maxFreq) {
28-
mode = num;
29-
maxFreq = freq;
28+
29+
for (const [number, frequency] of frequencies) {
30+
if (frequency > highestFrequency) {
31+
highestFrequency = frequency;
32+
mode = number;
3033
}
3134
}
3235

33-
return maxFreq === 0 ? NaN : mode;
36+
return highestFrequency === 0 ? NaN : mode;
37+
}
38+
39+
function calculateMode(list) {
40+
const frequencies = countFrequencies(list);
41+
return findMostFrequent(frequencies);
3442
}
3543

36-
module.exports = calculateMode;
44+
module.exports = calculateMode;

‎Sprint-2/stretch/till.js‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
// totalTill takes an object representing coins in a till
22

3-
// Given an object of coins
4-
// When this till object is passed to totalTill
5-
// Then it should return the total amount in pounds
6-
73
function totalTill(till) {
84
let total = 0;
95

106
for (const [coin, quantity] of Object.entries(till)) {
11-
total += coin * quantity;
7+
const coinValue = parseInt(coin, 10);
8+
total += coinValue * quantity;
129
}
1310

1411
return `£${total / 100}`;
1512
}
1613

17-
const till = {
18-
"1p": 10,
19-
"5p": 6,
20-
"50p": 4,
21-
"20p": 10,
22-
};
23-
const totalAmount = totalTill(till);
24-
25-
// a) What is the target output when totalTill is called with the till object
14+
// a) The target output is £4.4
2615

27-
// b) Why do we need to use Object.entries inside the for...of loop in this function?
16+
// b) Object.entries converts the object into key-value pairs,
17+
// allowing us to access both the coin and its quantity.
2818

29-
// c) What does coin * quantity evaluate to inside the for...of loop?
19+
// c) coinValue * quantity calculates the total value
20+
// of that type of coin.
3021

31-
// d) Write a test for this function to check it works and then fix the implementation of totalTill
22+
module.exports = totalTill;

‎Sprint-2/stretch/till.test.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const totalTill = require("./till.js");
2+
3+
test("calculates the total amount in the till", () => {
4+
const till = {
5+
"1p": 10,
6+
"5p": 6,
7+
"50p": 4,
8+
"20p": 10,
9+
};
10+
11+
expect(totalTill(till)).toBe("£4.4");
12+
});
13+
14+
test("returns £0 for an empty till", () => {
15+
expect(totalTill({})).toBe("£0");
16+
});

0 commit comments

Comments
 (0)