-
-
Notifications
You must be signed in to change notification settings - Fork 279
WM4 - Azin Yadegari - JavaScript -Core-1-Week3 #102
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 | ||
|
|
@@ -13,6 +13,12 @@ | |
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let tempRepo=[] | ||
| for (i=0 ; i < cities.length ; 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. I also often forget to declare variable -i in loops)) 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. What's missing from this line of code? |
||
| let temp=temperatureService(cities[i]) | ||
| tempRepo.push(`The temperature in ${cities[i]} is ${temp} degrees`) | ||
| } | ||
| return tempRepo | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,24 +6,62 @@ | |
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let acceptedTitles=[] | ||
| let lengthCheck=0 | ||
| for ( i=0 ; i < allArticleTitles.length ; 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. What's missing from this line of code? |
||
| lengthCheck= allArticleTitles[i].length | ||
| if (lengthCheck <= 65){ | ||
| acceptedTitles.push(allArticleTitles[i]) | ||
| } | ||
| } | ||
| return acceptedTitles | ||
| } | ||
|
|
||
| // function potentialHeadlines(allArticleTitles){ | ||
| // let acceptedTitles=[] | ||
| // for (element of allArticleTitles){ | ||
| // ( element.length <= 65 )? acceptedTitles.push(element):next | ||
| // } | ||
| // return acceptedTitles | ||
| // } | ||
|
|
||
| /* | ||
| 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) | ||
| Implement the function below, which returns the title with the fewest words.e | ||
| (you can assume words will always be separated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| } | ||
| let minValue | ||
| let indexOfShortestTitle | ||
| let wordCountOFTitles=[] | ||
| let i=0 | ||
| do { | ||
| let wordCount= allArticleTitles[i].trim().split(" ").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. Nice job calling
Author
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. thanks,to remove any blank space from both end , & prevent counting extra word :) |
||
| wordCountOFTitles.push(wordCount) | ||
| i++ | ||
| } | ||
| while(i < allArticleTitles.length) | ||
|
|
||
| minValue= Math.min(...wordCountOFTitles) | ||
|
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. Nice use of the spread operator! |
||
| indexOfShortestTitle=wordCountOFTitles.indexOf(minValue) | ||
|
|
||
| return allArticleTitles[indexOfShortestTitle] | ||
| } | ||
| /* | ||
| 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 | ||
| let titleWithNum=[] | ||
| for (const title of allArticleTitles){ | ||
| if (/\d/.test(title)){ | ||
|
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. test() - interesting method, I'll keep it in mind |
||
| titleWithNum.push(title) | ||
| } | ||
| } | ||
| return titleWithNum | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -32,6 +70,11 @@ function headlinesWithNumbers(allArticleTitles) { | |
| */ | ||
| function averageNumberOfCharacters(allArticleTitles) { | ||
| // TODO | ||
| let sum=0 | ||
| for (i=0 ; i <allArticleTitles.length;i++){ | ||
| sum += allArticleTitles[i].length | ||
| } | ||
| return Math.round(sum/allArticleTitles.length) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,8 +33,35 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Solve the smaller problems, and then build those solutions back up to solve the larger problem. | ||
| Functions can help with this! | ||
| */ | ||
| // function getAveragePrices(closingPricesForAllStocks) { | ||
| // // TODO | ||
| // let averageArray=[] | ||
| // for (const array of closingPricesForAllStocks){ | ||
| // let sum=0 | ||
| // let average=0 | ||
| // for (const value of array){ | ||
| // sum+=value | ||
| // } | ||
| // average= parseFloat((sum / closingPricesForAllStocks[i].length).toFixed(2)) | ||
| // averageArray.push(average) | ||
| // } | ||
| // return averageArray | ||
| // } | ||
|
|
||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let aveArray=[] | ||
| for (i=0 ; i< closingPricesForAllStocks.length;i++){ | ||
| let sum=0 | ||
| let average=0 | ||
| let j=0 | ||
| do{ | ||
|
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 did you chose a |
||
| sum+= closingPricesForAllStocks[i][j] | ||
| j++ | ||
| }while(j< closingPricesForAllStocks[i].length) | ||
| average= parseFloat((sum / closingPricesForAllStocks[i].length).toFixed(2)) | ||
| aveArray.push(average) | ||
| } | ||
| return aveArray | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -49,6 +76,13 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| */ | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| let priceChangeArray=[] | ||
| for(i=0 ; i< closingPricesForAllStocks.length ; i++){ | ||
| let priceChange=0 | ||
| priceChange =parseFloat((closingPricesForAllStocks[i][(closingPricesForAllStocks[i].length -1)]-closingPricesForAllStocks[i][0]).toFixed(2)) | ||
|
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. Can you think of a way to make this line of code more clear? |
||
| priceChangeArray.push(priceChange) | ||
| } | ||
| return priceChangeArray | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -60,11 +94,20 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| - Returns an array of strings describing what the highest price was for each stock. | ||
| For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33" | ||
| The test will check for this exact string. | ||
| The stock ticker should be capitalised. | ||
| The stock ticker should be capitalized. | ||
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let describingArray=[] | ||
| let max | ||
| let description | ||
| for (i=0 ; i< stocks.length ; i++){ | ||
| max= Math.max(...closingPricesForAllStocks[i]) | ||
| description= `The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${max.toFixed(2)}` | ||
| describingArray.push(description) | ||
| } | ||
| return describingArray | ||
| } | ||
|
|
||
|
|
||
|
|
||
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.
it looks simple and smart