-
-
Notifications
You must be signed in to change notification settings - Fork 279
London 9-Sana Asaf- JavaScript-Core-1-Coursework-Week3 #197
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 |
|---|---|---|
|
|
@@ -12,7 +12,10 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let temparatureStatements = []; | ||
| for (let i = 0; i < cities.length; i++){ | ||
|
Member
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 get the solutions for this week, take a look at the example solution. Yours works and is a correct answer; the mentor chose a different strategy. Can you think what the advantages of the mentor's choices are? |
||
| temparatureStatements[i]="The temperature in "+cities[i]+" is "+temperatureService(cities[i])+" degrees";} | ||
| return temparatureStatements; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,11 @@ function generateRandomNumber() { | |
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
|
Member
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 solution works well. Have you installed Prettier and run it in your VSCode? What does the style guide say about this? https://syllabus.codeyourfuture.io/guides/code-style-guide#using-prettier-to-format-code-automatically |
||
| // TODO - implement using a do-while loop | ||
| let i = 0; | ||
| do{ | ||
| i=generateRandomNumber(); | ||
| if (i>50) return(i); | ||
| }while(i<=50); | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,24 +6,65 @@ | |
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let checkedArticleTitles=[]; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
|
Member
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 works well and is a correct answer. When you look at the solutions, the mentor used a different kind of loop. Can you think why? |
||
| if (allArticleTitles[i].length<=65) { | ||
| checkedArticleTitles.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return checkedArticleTitles; | ||
| } | ||
|
|
||
| /* | ||
| 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) | ||
| let wordNums = [] | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| wordNums.push(allArticleTitles[i].split(' ').length) | ||
| } | ||
| return allArticleTitles[wordNums.indexOf(Math.min(...wordNums))] | ||
|
|
||
|
|
||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| let allArticleTitleswithspacecount=[]; | ||
| let minIndex; | ||
| /*for (i=0;i<allArticleTitles.length;i++) | ||
| { | ||
|
|
||
| allArticleTitleswithspacecount.push(allArticleTitles[i].split(' ').length); | ||
| } | ||
|
|
||
| minIndex=Math.min.apply(Math, allArticleTitleswithspacecount); | ||
| return(allArticleTitles[minIndex]); | ||
| let wordNums = []*/ | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| allArticleTitleswithspacecount.push(allArticleTitles[i].split(' ').length) | ||
| } | ||
| return allArticleTitles[allArticleTitleswithspacecount.indexOf(Math.min(...allArticleTitleswithspacecount))] | ||
|
|
||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 | ||
| let headlineWithNumberArr=[] | ||
| let length=allArticleTitles.length; | ||
| const numberPattern=/\d/; | ||
|
|
||
| for (let i = 0; i < length; i++) { | ||
| if (numberPattern.test(allArticleTitles[i])) { | ||
| headlineWithNumberArr.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return(headlineWithNumberArr); | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -32,6 +73,14 @@ function headlinesWithNumbers(allArticleTitles) { | |
| */ | ||
| function averageNumberOfCharacters(allArticleTitles) { | ||
| // TODO | ||
| let sum=0; | ||
| let average=0; | ||
| let length=allArticleTitles.length; | ||
| for (let i = 0; i < length; i++) { | ||
| sum+=allArticleTitles[i].length; | ||
| } | ||
| average = sum /length; | ||
| return(Math.round(average)); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| /* | ||
| THESE EXERCISES ARE QUITE HARD. JUST DO YOUR BEST, AND COME WITH QUESTIONS IF YOU GET STUCK :) | ||
|
|
||
|
|
||
| Imagine we a working for a finance company. Below we have: | ||
| - an array of stock tickers | ||
| - an array of arrays containing the closing price for each stock in each of the last 5 days. | ||
|
|
@@ -35,6 +36,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| /* let sum=0; | ||
|
Member
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 code seems to be commented out! |
||
| let averagePricesForAllStocks=[]; | ||
| let length=closingPricesForAllStocks.length; | ||
| for (let i = 0; i < length; i++) { | ||
| let jlength=closingPricesForAllStocks[i].length; | ||
| for (let j=0;j<jlength;j++){ | ||
| sum+=allArticleTitles[i][j]; | ||
| } | ||
| averagePricesForAllStocks.push(Math.round((sum/jlength)* 100) / 100); | ||
| } | ||
| return(averagePricesForAllStocks); | ||
| } | ||
|
|
||
| /* | ||
|
|
||
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.
Typo here! Typos in variable names often cause bugs so it's really worth picking them up. There is a spellchecker package you can install in VSCode https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker