-
-
Notifications
You must be signed in to change notification settings - Fork 279
London-Class-8-JavaScript-Core-1-Coursework-Week3-Timea-Reich #35
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 |
|---|---|---|
|
|
@@ -17,7 +17,12 @@ const BIRTHDAYS = [ | |
| ]; | ||
|
|
||
| function findFirstJulyBDay(birthdays) { | ||
| // TODO | ||
| let firstJulyBirthday= ''; | ||
| for (let i=0; i<birthdays.length; i++){ | ||
| if (birthdays[i] === 'July 11th') | ||
| firstJulyBirthday = birthdays[i]; | ||
| } | ||
| return firstJulyBirthday | ||
| } | ||
|
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 for for loop here |
||
|
|
||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,13 @@ let tubeStations = [ | |
| "Tottenham Court Road" | ||
| ]; | ||
|
|
||
| for (let i=0; i<tubeStations.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. Line 14 and 21 are for loops but not for-of loops as they do not contain the keyword "of". Could you replace these for loops with for-of loops. Examples of for-of loops can be found on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of |
||
| console.log(tubeStations[i]) | ||
| } | ||
|
|
||
|
|
||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||
| let str = "codeyourfuture"; | ||
| for (let i=0; i<str.length; i++){ | ||
| console.log(str[i].toLocaleUpperCase()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,10 +10,17 @@ function generateRandomNumber() { | |
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| let number; | ||
| do { | ||
| number = generateRandomNumber(); | ||
| } | ||
| while (number < 50) | ||
|
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. For this function, we want to return when the number is greater than 50 only. Is it possible for 50 to be returned and if so, could you change this line to only return greater than 50 and not 50? |
||
| return number; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
|
|
||
| // /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| test("Returned value should always be greater than 50", () => { | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,16 @@ | |
| 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 | ||
| let newArr =[]; | ||
| for (let i=0; i<allArticleTitles.length; i++){ | ||
| if (allArticleTitles[i].length <= 65 ){ | ||
| newArr.push(allArticleTitles[i]) | ||
| } | ||
| } | ||
| return newArr; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,25 +22,51 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| } | ||
|
|
||
| let countLength = allArticleTitles[0].split(" ").length; | ||
| let shortTitle; | ||
|
|
||
| for (let i=0; i<allArticleTitles.length; i++){ | ||
| if (countLength > allArticleTitles[i].split(" ").length) { | ||
| countLength = allArticleTitles[i].split(" ").length | ||
| console.log(allArticleTitles[i]) | ||
| shortTitle = allArticleTitles[i] | ||
| } | ||
| } | ||
| return shortTitle | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 newArr=[] | ||
| for (let i=0; i<allArticleTitles.length; i++){ | ||
| if (allArticleTitles[i].match(/[0-9]/g)){ | ||
| newArr.push(allArticleTitles[i]) | ||
| } | ||
| } | ||
| return newArr | ||
| } | ||
|
|
||
|
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 regex here |
||
|
|
||
| /* | ||
| 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 averageChar; | ||
| let sum= 0; | ||
| for (let i=0; i<allArticleTitles.length; i++){ | ||
| sum = sum + allArticleTitles[i].length; | ||
| averageChar= sum / allArticleTitles.length | ||
| } | ||
| return Math.round(averageChar) | ||
| } | ||
| // console.log(averageNumberOfCharacters(ARTICLE_TITLES)) | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -50,7 +84,7 @@ const ARTICLE_TITLES = [ | |
| "Brussels urges Chile's incoming president to endorse EU trade deal", | ||
| ]; | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| // /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| test("should only return potential headlines", () => { | ||
| expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,8 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let average =[] | ||
| let stockSum =0; | ||
| for (let i=0; i<closingPricesForAllStocks.length; i++){ | ||
| stockSum=0 | ||
| for (let j=0; j<closingPricesForAllStocks[i].length; j++){ | ||
|
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 names and readable code. great work Timea. |
||
| stockSum = stockSum + closingPricesForAllStocks[i][j]; | ||
| } | ||
| average.push(Number((stockSum/closingPricesForAllStocks[i].length).toFixed(2))) | ||
| } | ||
| return average | ||
| } | ||
| //console.log(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)) | ||
|
|
||
| /* | ||
| We also want to see what the change in price is from the first day to the last day for each stock. | ||
|
|
@@ -48,8 +58,16 @@ 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 priceChangeArr=[] | ||
|
|
||
| for (price of closingPricesForAllStocks) { | ||
| let priceChange = Number((price[price.length -1] - price[0]).toFixed(2)); | ||
| priceChangeArr.push(priceChange); | ||
| } | ||
| return priceChangeArr; | ||
| } | ||
|
|
||
| // console.log(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)) | ||
|
|
||
| /* | ||
| As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. | ||
|
|
@@ -64,11 +82,16 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| highestPriceLast5Days = []; | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| highestPriceLast5Days.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i]).toFixed(2)}`); | ||
| } | ||
| return highestPriceLast5Days; | ||
| } | ||
| //console.log(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS,STOCKS)) | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| // /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| test("should return the average price for each stock", () => { | ||
| expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( | ||
| [176.89, 335.66, 3405.66, 2929.22, 1041.93] | ||
|
|
||
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 checks for the 11th of July only, is there a way we could use includes method on arrays to check for any day in July?
Here's the docs for includes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes