-
-
Notifications
You must be signed in to change notification settings - Fork 283
London-10/Mahendra-Balal/JavaScript-core-1/Coursework/Week-3 #265
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,33 +5,69 @@ | |
Implement the function below, which will return a new array containing only article titles which will fit. | ||
*/ | ||
function potentialHeadlines(allArticleTitles) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation here looks good 👍 |
||
// TODO | ||
const ARTICLE_TITLES = []; | ||
for (let i = 0; i < allArticleTitles.length; i++) { | ||
if (allArticleTitles[i].length <= 65) { | ||
ARTICLE_TITLES.push(allArticleTitles[i]); | ||
}; | ||
|
||
|
||
} | ||
|
||
return ARTICLE_TITLES; | ||
} | ||
|
||
|
||
/* | ||
The editor of the FT likes short headlines with only a few words! | ||
Implement the function below, which returns the title with the fewest words. | ||
(you can assume words will always be seperated by a space) | ||
(you can assume words will always be separated by a space) | ||
*/ | ||
function titleWithFewestWords(allArticleTitles) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good attempt. I think there is one small problem with this code. |
||
// TODO | ||
let shortestTitle = ""; | ||
let shortestWordCount = Infinity; | ||
|
||
for (let i = 0; i < allArticleTitles.length; i++) { | ||
const words = allArticleTitles[i].split(""); | ||
const wordCount = words.length; | ||
if (wordCount < shortestWordCount) { | ||
shortestTitle = allArticleTitles[i]; | ||
shortestWordCount = wordCount; | ||
} | ||
} | ||
return shortestTitle; | ||
} | ||
|
||
/* | ||
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) { | ||
// TODO | ||
function findingNumber(str) { | ||
return /[0-9]/.test(str); | ||
} | ||
|
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job 👍 |
||
function headlinesWithNumbers(allArticleTitles) { | ||
let titleWithNumbers = []; | ||
for (let i = 0; i < allArticleTitles.length; i++) { | ||
if (findingNumber(allArticleTitles[i])) { | ||
titleWithNumbers.push(allArticleTitles[i]); | ||
} | ||
} | ||
return titleWithNumbers; | ||
} | ||
/* | ||
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) { | ||
mabalal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO | ||
let totalCharacter = 0; | ||
let totalTitles = allArticleTitles.length; | ||
|
||
for (let i = 0; i < totalTitles; i++) { | ||
totalCharacter += allArticleTitles[i].length; | ||
} | ||
return Math.round(totalCharacter / totalTitles); | ||
} | ||
|
||
|
||
|
@@ -49,7 +85,8 @@ const ARTICLE_TITLES = [ | |
"The three questions that dominate investment", | ||
"Brussels urges Chile's incoming president to endorse EU trade deal", | ||
]; | ||
|
||
// console.log(potentialHeadlines(ARTICLE_TITLES)); | ||
potentialHeadlines(ARTICLE_TITLES) | ||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
||
test("should only return potential headlines", () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks perfect! Good job 😄