-
-
Notifications
You must be signed in to change notification settings - Fork 279
Glasgow 6 - Malkit Benning - JS Core 1 Week 3 #225
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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| Imagine we're making a weather app! | ||
|
|
||
| We have a list of cities that the user wants to track. | ||
| We also already have a temperatureService function which will take a city as a parameter and return a temparature. | ||
| We also already have a temperatureService function which will take a city as a parameter and return a temperature. | ||
|
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. Praise: Good Spot |
||
|
|
||
| Implement the function below: | ||
| - take the array of cities as a parameter | ||
|
|
@@ -12,52 +12,50 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| const weatherInCities = []; | ||
| for (const city of cities) { | ||
| let cityTemperature = temperatureService(city); | ||
| let cityStatement = `The temperature in ${city} is ${cityTemperature} degrees`; | ||
| weatherInCities.push(cityStatement); | ||
| } | ||
| return weatherInCities; | ||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| function temperatureService(city) { | ||
| let temparatureMap = new Map(); | ||
|
|
||
| temparatureMap.set('London', 10); | ||
| temparatureMap.set('Paris', 12); | ||
| temparatureMap.set('Barcelona', 17); | ||
| temparatureMap.set('Dubai', 27); | ||
| temparatureMap.set('Mumbai', 29); | ||
| temparatureMap.set('São Paulo', 23); | ||
| temparatureMap.set('Lagos', 33); | ||
| return temparatureMap.get(city); | ||
| let temparatureMap = new Map(); | ||
|
|
||
| temparatureMap.set("London", 10); | ||
| temparatureMap.set("Paris", 12); | ||
| temparatureMap.set("Barcelona", 17); | ||
| temparatureMap.set("Dubai", 27); | ||
| temparatureMap.set("Mumbai", 29); | ||
| temparatureMap.set("São Paulo", 23); | ||
| temparatureMap.set("Lagos", 33); | ||
|
|
||
| return temparatureMap.get(city); | ||
| } | ||
|
|
||
| test("should return a temperature report for the user's cities", () => { | ||
| let usersCities = [ | ||
| "London", | ||
| "Paris", | ||
| "São Paulo" | ||
| ] | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in London is 10 degrees", | ||
| "The temperature in Paris is 12 degrees", | ||
| "The temperature in São Paulo is 23 degrees" | ||
| ]); | ||
| let usersCities = ["London", "Paris", "São Paulo"]; | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in London is 10 degrees", | ||
| "The temperature in Paris is 12 degrees", | ||
| "The temperature in São Paulo is 23 degrees", | ||
| ]); | ||
| }); | ||
|
|
||
| test("should return a temperature report for the user's cities (alternate input)", () => { | ||
| let usersCities = [ | ||
| "Barcelona", | ||
| "Dubai" | ||
| ] | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in Barcelona is 17 degrees", | ||
| "The temperature in Dubai is 27 degrees" | ||
| ]); | ||
| let usersCities = ["Barcelona", "Dubai"]; | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in Barcelona is 17 degrees", | ||
| "The temperature in Dubai is 27 degrees", | ||
| ]); | ||
| }); | ||
|
|
||
| test("should return an empty array if the user hasn't selected any cities", () => { | ||
| expect(getTemperatureReport([])).toEqual([]); | ||
| }); | ||
| expect(getTemperatureReport([])).toEqual([]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,77 +5,122 @@ | |||||
| Implement the function below, which will return a new array containing only article titles which will fit. | ||||||
| */ | ||||||
| function potentialHeadlines(allArticleTitles) { | ||||||
| // TODO | ||||||
| const charLimit = 65; | ||||||
|
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. Excellent - great use of a variable name |
||||||
| const articlesUnderLimit = []; | ||||||
|
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 variable name |
||||||
|
|
||||||
| for (const articleTitle of 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. Suggestion: What you've done is fine, but it would also be appropriate to use a filter here such as articlesUnderLimit = allArticleTitles.filter((articleTitle) => articleTitle <= charLimit); |
||||||
| if (articleTitle.length <= charLimit) { | ||||||
| articlesUnderLimit.push(articleTitle); | ||||||
| } | ||||||
| } | ||||||
| return articlesUnderLimit; | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| 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) { | ||||||
| // TODO | ||||||
| // TODO | ||||||
|
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. Nitpick: Remove the TODO comments once you've done the work, in production code TODO comments are often used to note places where further work still needs to be done so leaving it in can cause confusion |
||||||
| let shortestTitle = ""; | ||||||
| let shortestSpaceCount = 0; | ||||||
| for (const title of allArticleTitles) { | ||||||
| if (shortestTitle === "") { | ||||||
|
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. Question: Why do we need to have a check for blank strings specifically? |
||||||
| shortestTitle = title; | ||||||
| shortestSpaceCount = title.split(" ").length - 1; | ||||||
| } else { | ||||||
| let currentSpaceCount = title.split(" ").length - 1; | ||||||
| if (currentSpaceCount < shortestSpaceCount) { | ||||||
| shortestTitle = title; | ||||||
| shortestSpaceCount = currentSpaceCount; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| 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 containsNumbers(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. Praise: Good separation of logic into a new function |
||||||
| 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. Suggestion: This works and is OK, however in the scope of work in this course I'd recommend avoiding regex's. Instead this could be done by using the string includes function. |
||||||
| } | ||||||
|
|
||||||
| function headlinesWithNumbers(allArticleTitles) { | ||||||
| // TODO | ||||||
| let numberArticles = []; | ||||||
| for (const title of allArticleTitles) { | ||||||
| if (containsNumbers(title)) { | ||||||
| numberArticles.push(title); | ||||||
| } | ||||||
| } | ||||||
| return numberArticles; | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| 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) { | ||||||
| // TODO | ||||||
| // TODO | ||||||
| let charCount = 0; | ||||||
| const articleTitleCount = allArticleTitles.length; | ||||||
|
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. Don't generally need to store |
||||||
| for (const articleTitle of 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.
Suggested change
|
||||||
| charCount += articleTitle.length; | ||||||
| } | ||||||
| const averageCharCount = Math.round(charCount / articleTitleCount); | ||||||
| return averageCharCount; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| /* ======= 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", | ||||||
| "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" | ||||||
| ])); | ||||||
| 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([]); | ||||||
| expect(potentialHeadlines([])).toEqual([]); | ||||||
| }); | ||||||
|
|
||||||
| test("should return the title with the fewest words", () => { | ||||||
| expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual("The three questions that dominate investment"); | ||||||
| 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" | ||||||
| ])); | ||||||
| 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); | ||||||
| expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); | ||||||
| }); | ||||||
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.
Remember to remove old comments. Remember you still have old versions of your code in your version history - so you should be able to inspect the old version if you want to.