-
-
Notifications
You must be signed in to change notification settings - Fork 279
London_10-Saliha_Popal-JavaScript-Core-1-Coursework-Week3 #231
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. | ||
|
|
||
| Implement the function below: | ||
| - take the array of cities as a parameter | ||
|
|
@@ -11,25 +11,29 @@ | |
| - Hint: you can call the temperatureService function from your function | ||
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| } | ||
|
|
||
| function getTemperatureReport(cities){ | ||
| const statement = []; | ||
|
Contributor
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. Another small point - is |
||
| for(const city of cities){ | ||
| const temperature = temperatureService(city) | ||
| statement.push(`The temperature in ${city} is ${temperature} degrees`) | ||
| } | ||
| return statement; | ||
| } | ||
|
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. Comment: Well done! Consider: You could also iterate [i] |
||
|
|
||
| /* ======= 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); | ||
| let temperatureMap = new Map(); | ||
|
|
||
| temperatureMap.set('London', 10); | ||
| temperatureMap.set('Paris', 12); | ||
| temperatureMap.set('Barcelona', 17); | ||
| temperatureMap.set('Dubai', 27); | ||
| temperatureMap.set('Mumbai', 29); | ||
| temperatureMap.set('São Paulo', 23); | ||
| temperatureMap.set('Lagos', 33); | ||
|
|
||
| return temparatureMap.get(city); | ||
| return temperatureMap.get(city); | ||
| } | ||
|
|
||
| test("should return a temperature report for the user's cities", () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,36 +4,145 @@ | |
| 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) { | ||
| // TODO | ||
| } | ||
|
|
||
| /* | ||
| // function potentialHeadlines(allArticleTitles) { | ||
| // // Create an empty array to store the potential headlines | ||
| // const potentialHeadlines = []; | ||
|
|
||
| // // Loop through each article title in the input array | ||
| // for (let i = 0; i < allArticleTitles.length; i++) { | ||
| // const articleTitle = allArticleTitles[i]; | ||
|
|
||
| // // Check if the title is 65 characters or less | ||
| // if (articleTitle.length <= 65) { | ||
| // // If it is, add it to the potential headlines array | ||
| // potentialHeadlines.push(articleTitle); | ||
| // } | ||
| // } | ||
|
|
||
| // // Return the array of potential headlines | ||
| // return potentialHeadlines; | ||
| // } | ||
|
|
||
|
|
||
| // Using filter function with loops | ||
|
|
||
| // function filterPotentialHeadlines(allArticleTitles){ | ||
| // let potentialHeadlines = []; | ||
| // for(let title of allArticleTitles){ | ||
| // if(title.length <= 65){ | ||
| // potentialHeadlines.push(title); | ||
| // } | ||
| // } | ||
| // return potentialHeadlines; | ||
| // } | ||
|
|
||
| // const allArticleTitles = [ "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", | ||
| // "Labor 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 Elan 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",] | ||
| // console.log(filterPotentialHeadlines(allArticleTitles)); | ||
|
|
||
|
|
||
| // Using the filter function without loops | ||
|
|
||
|
|
||
|
Contributor
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 looks perfect, well done 😄 |
||
| function isPotentialHeadline(articleTitle){ | ||
| return articleTitle.length <= 65; | ||
| } | ||
|
|
||
| function filterPotentialHeadlines(articleTitles){ | ||
| // Here filter takes the function as a parameter | ||
| return articleTitles.filter(isPotentialHeadline); | ||
| } | ||
|
|
||
| const articleTitles = [ "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", | ||
| "Labor 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 Elan 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",] | ||
|
|
||
| console.log(filterPotentialHeadlines(articleTitles)); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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 | ||
| function titleWithFewestWords(allArticleTitles) { | ||
|
|
||
| let fewestWords = allArticleTitles[0]; | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| if (fewestWords.length > allArticleTitles[i].length) { | ||
| fewestWords = allArticleTitles[i]; | ||
| } | ||
| } | ||
|
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. Comment: Same code solution as mine |
||
|
|
||
| return fewestWords; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| The editor of the FT has realised that headlines which have numbers in them get more clicks! | ||
| The editor of the FT has realized 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 | ||
| } | ||
| const headlinesWithNumbers = []; | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| const articleTitle = allArticleTitles[i]; | ||
|
|
||
| for (let j = 0; j < articleTitle.length; j++) { | ||
| const character = articleTitle.charAt(j); | ||
|
|
||
| if (!isNaN(parseInt(character))) { | ||
| headlinesWithNumbers.push(articleTitle); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return headlinesWithNumbers; | ||
| } | ||
|
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. Comment: Interesting. I see you used .charAt and NaN. I have not seen any other use it. My solution: return newArr; |
||
|
|
||
|
|
||
| /* | ||
| 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) { | ||
|
Contributor
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. Very nice solution! |
||
| // TODO | ||
| } | ||
| let totalChars = 0; | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| totalChars += allArticleTitles[i].length; | ||
| } | ||
|
|
||
| const averageChars = Math.round(totalChars / allArticleTitles.length); | ||
|
|
||
| return averageChars; | ||
| } | ||
|
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. Comment: I see you use just 1 global variable (let). Mine has 2. Well done |
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= List of Articles - DO NOT MODIFY ===== */ | ||
|
|
@@ -42,9 +151,9 @@ const ARTICLE_TITLES = [ | |
| "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", | ||
| "Labor 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", | ||
| "Chinese social media users blast Elan 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", | ||
|
|
@@ -53,16 +162,16 @@ const ARTICLE_TITLES = [ | |
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| test("should only return potential headlines", () => { | ||
| expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([ | ||
| expect(new Set(filterPotentialHeadlines(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", | ||
| "Labor 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(filterPotentialHeadlines([])).toEqual([]); | ||
| }); | ||
|
|
||
| test("should return the title with the fewest words", () => { | ||
|
|
||
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 good 👍
One small suggestion - keep an eye on indentation, as it will make it easier for other developers to read your code.