Skip to content

Commit 0fbe032

Browse files
committed
stretch
1 parent a11bae4 commit 0fbe032

5 files changed

Lines changed: 68 additions & 8 deletions

File tree

Sprint-2/stretch/count-words.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
/*
22
Count the number of times a word appears in a given string.
33
4+
45
Write a function called countWords that
56
- takes a string as an argument
67
- returns an object where
78
- the keys are the words from the string and
89
- the values are the number of times the word appears in the string
910
11+
1012
Example
1113
If we call countWords like this:
1214
15+
1316
countWords("you and me and you") then the target output is { you: 2, and: 2, me: 1 }
1417
18+
1519
To complete this exercise you should understand
1620
- Strings and string manipulation
1721
- Loops
1822
- Comparison inside if statements
1923
- Setting values on an object
2024
25+
2126
## Advanced challenges
2227
28+
2329
1. Remove all of the punctuation (e.g. ".", ",", "!", "?") to tidy up the results
2430
31+
2532
2. Ignore the case of the words to find more unique words. e.g. (A === a, Hello === hello)
2633
34+
2735
3. Order the results to find out which word is the most common in the input
2836
*/
37+
38+
39+
40+
function countWords(str) {
41+
const counts = {};
42+
43+
const words = str.split(" ").filter((w) => w !== "");
44+
45+
for (const word of words) {
46+
if (counts[word]) {
47+
counts[word] += 1;
48+
} else {
49+
counts[word] = 1;
50+
}
51+
}
52+
53+
return counts;
54+
}
55+

Sprint-2/stretch/mode.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
// You are given an implementation of calculateMode
22

3+
34
// calculateMode's implementation can be broken down into two stages:
45

6+
57
// Stage 1. One part of the code tracks the frequency of each value
68
// Stage 2. The other part finds the value with the highest frequency
79

10+
811
// refactor calculateMode by splitting up the code
912
// into smaller functions using the stages above
1013

11-
function calculateMode(list) {
12-
// track frequency of each value
13-
let freqs = new Map();
1414

15-
for (let num of list) {
15+
function getFrequencies(list) {
16+
const freqs = new Map();
17+
18+
for (const num of list) {
1619
if (typeof num !== "number") {
1720
continue;
1821
}
1922

2023
freqs.set(num, (freqs.get(num) || 0) + 1);
2124
}
2225

23-
// Find the value with the highest frequency
26+
return freqs;
27+
}
28+
29+
function getModeFromFrequencies(freqs) {
2430
let maxFreq = 0;
2531
let mode;
26-
for (let [num, freq] of freqs) {
32+
33+
for (const [num, freq] of freqs) {
2734
if (freq > maxFreq) {
2835
mode = num;
2936
maxFreq = freq;
@@ -33,4 +40,9 @@ function calculateMode(list) {
3340
return maxFreq === 0 ? NaN : mode;
3441
}
3542

43+
function calculateMode(list) {
44+
const freqs = getFrequencies(list);
45+
return getModeFromFrequencies(freqs);
46+
}
47+
3648
module.exports = calculateMode;

Sprint-2/stretch/mode.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
const calculateMode = require("./mode.js");
22

3+
34
// Acceptance criteria for calculateMode function
45

6+
57
// Given an array of numbers
68
// When calculateMode is called on the array
79
// Then it should return the number that appears most frequently in the array
810

11+
912
// Example:
1013
// Given [2,4,1,2,3,2,1]
1114
// When calculateMode is called on [2,4,1,2,3,2,1]
1215
// Then it should return 2 */
1316

17+
1418
describe("calculateMode()", () => {
1519
test("returns the most frequent number in an array", () => {
1620
const nums = [2, 4, 1, 2, 3, 2, 1];

Sprint-2/stretch/till.js

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

3+
34
// Given an object of coins
45
// When this till object is passed to totalTill
56
// Then it should return the total amount in pounds
67

8+
79
function totalTill(till) {
810
let total = 0;
911

1012
for (const [coin, quantity] of Object.entries(till)) {
11-
total += coin * quantity;
13+
const valueInPence = Number(coin.replace("p", ""));
14+
total += valueInPence * quantity;
1215
}
1316

1417
return ${total / 100}`;
1518
}
1619

20+
1721
const till = {
1822
"1p": 10,
1923
"5p": 6,
@@ -22,10 +26,17 @@ const till = {
2226
};
2327
const totalAmount = totalTill(till);
2428

29+
2530
// a) What is the target output when totalTill is called with the till object
31+
// "£4.4"
32+
2633

2734
// b) Why do we need to use Object.entries inside the for...of loop in this function?
35+
// Object.entries returns [coin, quantity] pairs, allowing destructuring in the loop
36+
2837

2938
// c) What does coin * quantity evaluate to inside the for...of loop?
39+
// Original: "1p" * 10 = NaN. Fixed: 1 * 10 = 10 (pence for that coin type)
40+
3041

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

Sprint-2/stretch/totalTill.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const totalTill = require("./totalTill.js");
2+
3+
test("calculates total till value in pounds", () => {
4+
const till = { "1p": 10, "5p": 6, "50p": 4, "20p": 10 };
5+
expect(totalTill(till)).toBe("£4.4");
6+
});

0 commit comments

Comments
 (0)