-
-
Notifications
You must be signed in to change notification settings - Fork 279
London -10/Anu Thapaliya/JS-Core 1-Week3/exercises and mandatory done #252
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| /* | ||
|
|
||
| Imagine we're making a weather app! | ||
|
|
||
| We have a list of cities that the user wants to track. | ||
|
|
@@ -12,8 +13,19 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| forecast = [] | ||
|
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. it looks like the result is going to be "underfind". You need ro use special wording to declare a variable. |
||
| for(city of cities){ | ||
|
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. same is here, "city" is not declared yet |
||
| temperature = temperatureService(city); | ||
| information = `The temperature in ${city} is ${temperature} degrees`; | ||
| forecast.push(information); | ||
| } | ||
| return forecast; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,37 +5,85 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let titleCharacters = []; | ||
|
|
||
| for (let title of allArticleTitles) { | ||
|
|
||
| if (title.length<=65) { | ||
| titleCharacters.push(title); | ||
|
|
||
| } | ||
|
|
||
| } | ||
| return titleCharacters; | ||
| } | ||
|
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. in my opinion it is perfect code :) |
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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) | ||
| */ | ||
| function titleWithFewestWords(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. This looks almost perfect to me! A couple of very minor points:
|
||
| // TODO | ||
|
|
||
| let shortHeadlines; | ||
| let fewestWords; | ||
|
|
||
| for (let title of allArticleTitles) { | ||
| let numberOfWords = title.split(" ").length; | ||
|
|
||
| if (fewestWords === undefined || numberOfWords < fewestWords) { | ||
| fewestWords = numberOfWords; | ||
| shortHeadlines = title; | ||
| } | ||
| } | ||
| return shortHeadlines; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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. i like this solution! well done 👍 |
||
| } | ||
| function headlinesWithNumbers(allArticleTitles){ | ||
| let titleWithNumbers = []; | ||
| for (let headline of allArticleTitles) { | ||
| if (findingNumber(headline)=== true){ | ||
|
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. When you have an if statement that looks like this: |
||
| titleWithNumbers.push(headline); | ||
| } | ||
|
|
||
| } | ||
| return titleWithNumbers; | ||
|
|
||
| } | ||
|
|
||
|
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. I think it is better have function separately, not one inside another, so you can use them any time. |
||
|
|
||
| /* | ||
| 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 | ||
| let total =0; | ||
| let numberOfArticles = allArticleTitles.length; | ||
| for (let headline of allArticleTitles){ | ||
| total = total + headline.length; | ||
| } | ||
| let averageNumberOfWords = Math.round(total/numberOfArticles); | ||
|
|
||
| return averageNumberOfWords; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= List of Articles - DO NOT MODIFY ===== */ | ||
| const ARTICLE_TITLES = [ | ||
| "Streaming wars drive media groups to spend more than $100bn on new content", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,8 +34,25 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
|
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 good to me - great job 👍 |
||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| } | ||
| let averagePrices = []; | ||
|
|
||
| for(let types of closingPricesForAllStocks){ | ||
| let total = 0; | ||
| let average = 0; | ||
|
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. maybe it would be better to declare "average" variable outside the "for" function, as it is not used inside it, but outside |
||
| for(let i of types) { | ||
| total +=i; | ||
| } | ||
|
|
||
| average = Math.round(total/types.length *100)/100; | ||
| averagePrices.push(average); | ||
|
|
||
|
|
||
| } | ||
| return averagePrices; | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| /* | ||
| We also want to see what the change in price is from the first day to the last day for each stock. | ||
|
|
@@ -48,7 +65,14 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| The price change value should be rounded to 2 decimal places, and should be a number (not a string) | ||
| */ | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| let changesInPrice = []; | ||
| for (let types of closingPricesForAllStocks) { | ||
| let firstDayPrice = types[0]; | ||
| let lastDayPrice = types[types.length-1]; | ||
|
|
||
| changesInPrice.push(Math.round((lastDayPrice - firstDayPrice)*100)/100); | ||
|
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. why do we need here to multiply and the then divide by 100?
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. Multiplying by 100, using Math.round, and then dividing by 100 is one way to round a number to 2 decimal places in JavaScript. This page goes over a couple of ways you can round a number to 2 decimal places: https://linuxhint.com/round-number-to-2-decimal-places-javascript/ |
||
| } | ||
| return changesInPrice; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +88,16 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let financialReport = []; | ||
| let highestPrice = 0; | ||
| let stock = 0; | ||
| for (let types of closingPricesForAllStocks) { | ||
| highestPrice = Math.max(...types).toFixed(2); | ||
| financialReport.push (`The highest price of ${stocks[stock].toUpperCase()} in the last 5 days was ${highestPrice}`); | ||
| stock++; | ||
|
|
||
| } | ||
| return financialReport; | ||
| } | ||
|
|
||
|
|
||
|
|
||
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 implementation looks good 👍
As mentioned below, you may just need to check that you're using
letorconstwhen declaring a new variable. Even though the code works without it, it's a good habit to get into - otherwise we'll run into problems when we work with larger code bases 😄