-
-
Notifications
You must be signed in to change notification settings - Fork 279
ZA2 - Anthony Mogotlane - JavaScript-Core-1-Coursework - W3 #53
base: main
Are you sure you want to change the base?
Changes from all commits
ed25ae9
7e543ed
f34cfee
c30d436
599b4d8
a385259
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,7 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| return cities.map(town => `The temperature in ${town} is ${temperatureService(town)} degrees`) | ||
|
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 |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,15 @@ function generateRandomNumber() { | |
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| let rand = generateRandomNumber(); | ||
|
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. The function |
||
| do { | ||
| if(rand > 50) { | ||
| return rand; | ||
| } | ||
| } while(rand <= 50) | ||
| return rand; | ||
| } | ||
| console.log(getRandomNumberGreaterThan50()); | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| return allArticleTitles.filter(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. Good use of |
||
| if(title.length <= 65) return title; | ||
| }) | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,24 +16,26 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| let num = Math.min(...allArticleTitles.map(title => title.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. This is pretty cool, well done |
||
| return allArticleTitles.filter(title => (title.split(" ").length === num) ? title : "").join(""); | ||
| } | ||
|
|
||
| /* | ||
| 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) | ||
| 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 | ||
| return allArticleTitles.filter(titleWithNum => (/\d/.test(titleWithNum)) ? titleWithNum : ""); | ||
| } | ||
|
|
||
| /* | ||
| 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. | ||
| 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 arr = allArticleTitles.map(title => title.split("").length); | ||
| return Math.round(arr.reduce((tot, num) => tot + num)/arr.length); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,38 +34,45 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| return closingPricesForAllStocks.map(priceArr => { | ||
|
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. Concise, I like it! |
||
| return Number((priceArr.reduce((tot, num) => tot + num) / priceArr.length).toFixed(2)); | ||
| }) | ||
| } | ||
|
|
||
| /* | ||
| We also want to see what the change in price is from the first day to the last day for each stock. | ||
| Implement the below function, which | ||
| - Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays) | ||
| - Returns an array containing the price change over the last 5 days for each stock. | ||
| For example, the first element of the resulting array should contain Apple’s (aapl) price change for the last 5 days. | ||
| In this example it would be: | ||
| (Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2 | ||
| - Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays) | ||
| - Returns an array containing the price change over the last 5 days for each stock. | ||
| For example, the first element of the resulting array should contain Apple’s (aapl) price change for the last 5 days. | ||
| In this example it would be: | ||
| (Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2 | ||
| The price change value should be rounded to 2 decimal places, and should be a number (not a string) | ||
| */ | ||
| */ | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| return closingPricesForAllStocks.map(priceArr => { | ||
| return Number((priceArr[priceArr.length - 1] - priceArr[0]).toFixed(2)); | ||
| }) | ||
| } | ||
|
|
||
| /* | ||
| As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. | ||
| Implement the below function, which | ||
| - Takes 2 parameters: | ||
| - the CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays) | ||
| - the STOCKS array | ||
| - 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. | ||
| - Takes 2 parameters: | ||
| - the CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays) | ||
| - the STOCKS array | ||
| - 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 price should be shown with exactly 2 decimal places. | ||
| */ | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| return closingPricesForAllStocks.map((priceArr, i) => { | ||
| return `The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...priceArr).toFixed(2)}`; | ||
| }) | ||
| } | ||
| console.log(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)); | ||
|
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. Really well done on your solutions for this exercise Anthony 🔥 |
||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,10 @@ | |
| Using a loop, complete the function below so it returns the factorial of the number being passed in. | ||
| */ | ||
|
|
||
| // Solved it using recursion instead of a loop | ||
|
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. 👀 Much more elegant than the official solution 🤣 |
||
| function factorial(input) { | ||
| // TODO | ||
| if(input === 1) return 1; | ||
| return input * factorial(input - 1); | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
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.
Nicely done Anthony!