This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy path2-financial-times.js
More file actions
118 lines (99 loc) · 4.5 KB
/
2-financial-times.js
File metadata and controls
118 lines (99 loc) · 4.5 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
111
112
113
114
115
116
117
118
/*
Imagine you are working on the Financial Times web site! They have a list of article titles stored in an array.
The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less.
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
let headlines=[];
for (let title of allArticleTitles )
if(title.length <= 65){
headlines.push(title);
}
return headlines;
// TODO
}function titleWithFewestWords(allArticleTitles) {
let fewestWordsSoFar;
let titleWithFewestWords;
for(let title of allArticleTitles) {
// working out the number of words in the title by splitting on the space character
// this will generate an array. Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
let numWords = title.split(' ').length;
if(fewestWordsSoFar === undefined || numWords < fewestWordsSoFar) {
fewestWordsSoFar = numWords;
titleWithFewestWords = title;
}
}
return titleWithFewestWords;
}
/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
let articlesWithNumbers = [];
for(let title of allArticleTitles) {
// Making use of the new function created below
if(doesTitleContainANumber(title)) {
articlesWithNumbers.push(title);
}
}
return articlesWithNumbers;
}
// Creating another function to help break this problem down into smaller parts
function doesTitleContainANumber(title) {
for(let character of title) {
if(character >= '0' && character <= '9') {
return true;
}
}
return false;
}
/*
The Financial Times wants to understand what the average number of characters in an article title is.
Implement the function below to return this number - rounded to the nearest integer.
*/
function averageNumberOfCharacters(allArticleTitles) {
let totalCharacters = 0;
for(let title of allArticleTitles) {
totalCharacters += title.length;
}
return Math.round(totalCharacters / allArticleTitles.length);
}
/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
"Amazon Prime Video India country head: streaming is driving a TV revolution",
"Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights",
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"Chinese social media users blast Elon Musk over near miss in space",
"Companies raise over $12tn in 'blockbuster' year for global capital markets",
"The three questions that dominate investment",
"Brussels urges Chile's incoming president to endorse EU trade deal",
];
/* ======= TESTS - DO NOT MODIFY ===== */
test("should only return potential headlines", () => {
expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"The three questions that dominate investment"
]));
});
test("should return an empty array for empty input", () => {
expect(potentialHeadlines([])).toEqual([]);
});
test("should return the title with the fewest words", () => {
expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual("The three questions that dominate investment");
});
test("should only return headlines containing numbers", () => {
expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual(new Set([
"Streaming wars drive media groups to spend more than $100bn on new content",
"Companies raise over $12tn in 'blockbuster' year for global capital markets"
]));
});
test("should return the average number of characters in a headline", () => {
expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65);
});