-
-
Notifications
You must be signed in to change notification settings - Fork 279
Glasgow Class 6 - Mandy Cheung - JS1 - Week 3 #254
base: main
Are you sure you want to change the base?
Changes from all commits
ccb71f2
1132bf2
ed4c5a0
82efe29
a793419
d79840c
2d97392
1cc9545
9dd94d1
dd87c0e
b7c0abf
a704108
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 |
|---|---|---|
|
|
@@ -28,6 +28,13 @@ const AGES = [ | |
|
|
||
| // TODO - Write for loop code here | ||
|
|
||
| for (i = 0; i < WRITERS.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. Issue: The iterator variable |
||
| let writersName = WRITERS[i]; | ||
| let writersAge = AGES[i]; | ||
|
|
||
| console.log(`${writersName} is ${writersAge} years old!`); | ||
| } | ||
|
|
||
| /* | ||
| The output should look something like this: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,18 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| } | ||
| let temperatureReportResults = [ ]; | ||
|
|
||
| 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. Suggestion: Use a |
||
| let city = cities[i]; | ||
| let temperature = temperatureService(city); | ||
| let reportPhrasing = `The temperature in ${city} is ${temperature} degrees`; | ||
|
|
||
| temperatureReportResults.push(reportPhrasing); | ||
| } | ||
|
|
||
| return temperatureReportResults; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,19 @@ | ||
| /* | ||
| Imagine you are working on the Financial Times web site! They have a list of article titles stored in an array. | ||
| Imagine you are working on the Financial Times website! They have a list of article titles stored in an array. | ||
|
|
||
| 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 titlesShortEnough = [ ]; | ||
|
|
||
| for (const title of allArticleTitles) { | ||
| if(title.length <= 65) { | ||
| titlesShortEnough.push(title); | ||
| } | ||
| } | ||
|
|
||
| return titlesShortEnough; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,24 +22,59 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| } | ||
|
|
||
| let smallestTitle = allArticleTitles[0]; | ||
|
|
||
| for (i = 1; 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. Issue: The iterator variable for (let i = 1; i < allArtivlesTitles.length; i++)Suggestion: Use a for...of loop instead of a for loop to improve readability |
||
|
|
||
| if (smallestTitle.split(" ").length > allArticleTitles[i].split(" ").length) { | ||
| smallestTitle = allArticleTitles[i]; | ||
| } | ||
| } | ||
| return smallestTitle; | ||
| } | ||
| /* | ||
| 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 headlinesWithNumbers = [ ]; | ||
| const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
|
|
||
| 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. Issue: The iterator variable |
||
| if(numbers.some(element => allArticleTitles[i].includes(element))) { | ||
| headlinesWithNumbers.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return headlinesWithNumbers; | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
| // To store the total characters per title | ||
| let totalCharactersPerTitle = [ ]; | ||
|
|
||
| // To count the characters per title and add them to totalCharactersPerTitle | ||
| 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. Issue: The iterator variable |
||
| let countCharacters = allArticleTitles[i].length; | ||
| totalCharactersPerTitle.push(countCharacters); | ||
| } | ||
|
|
||
| // To store the sum of total characters | ||
| let totalCharacters = 0; | ||
| totalCharactersPerTitle.forEach( item => { | ||
|
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. Suggestion: This implementation is fine, but this could also be done with a reducer. e.g. let totalCharaters = totalCharactersPerTitle.reduce((iterator, element) => iterator + element); |
||
| totalCharacters += item; | ||
| }) | ||
|
|
||
| // To work out the average and round to the nearest integer | ||
| let averageNumberOfCharacters = Math.round(totalCharacters / (allArticleTitles.length)); | ||
|
|
||
| return averageNumberOfCharacters; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +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: | ||
| Imagine we are working for a finance company. Below we have: | ||
|
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. Praise: good spot :) |
||
| - an array of stock tickers | ||
| - an array of arrays containing the closing price for each stock in each of the last 5 days. | ||
| For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for STOCKS[2] (which is amzn) | ||
|
|
@@ -34,7 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| // for storing averages of each company's stock over 5 days | ||
| let averagePriceOverFiveDays = [ ]; | ||
|
|
||
| // This loop cycles through each stock, the i is the 5 day stock for AAPL, MSFT etc. | ||
| for (i = 0; i < closingPricesForAllStocks.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. Issue: The iterator variable |
||
| let totalStock = 0; | ||
| closingPricesForAllStocks[i].forEach((element) => { | ||
|
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. Suggestion: Instead of doing |
||
| totalStock += element; | ||
| }); | ||
| let calculateAverage = totalStock / closingPricesForAllStocks[i].length; | ||
| let roundAverage = Math.round(calculateAverage * 100) / 100; | ||
| averagePriceOverFiveDays.push(roundAverage); | ||
| } | ||
|
|
||
| return averagePriceOverFiveDays; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -47,10 +61,28 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| (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 | ||
|
|
||
|
|
||
| function getPriceChanges(array) { | ||
|
|
||
| let changeInPrice = [ ]; | ||
|
|
||
| for (i = 0; i < array.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. Issue: The iterator variable |
||
| let closingPricesArray = array; | ||
| let lengthOfInnerArray = closingPricesArray[i].length; | ||
| let fifthDayPrice = closingPricesArray[i][lengthOfInnerArray - 1]; | ||
| let firstDayPrice = closingPricesArray[i][0]; | ||
|
|
||
| let fifthDayMinusFirstDayRounded = Math.round((fifthDayPrice - 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. Suggestion do |
||
|
|
||
| changeInPrice.push(fifthDayMinusFirstDayRounded); | ||
| } | ||
|
|
||
| return changeInPrice; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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 | ||
|
|
@@ -64,7 +96,28 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let maxPrices = [ ]; | ||
| let stockNames = stocks; | ||
| let maxStockDescriptions = [ ]; | ||
|
|
||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| let currentMax = closingPricesForAllStocks[i][0]; | ||
|
|
||
| for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { | ||
| let currentElement = closingPricesForAllStocks[i][j]; | ||
|
|
||
| if (currentElement >= currentMax) { | ||
| currentMax = currentElement; | ||
| } | ||
| } | ||
|
|
||
| let capitaliseStockNames = stockNames[i].toUpperCase(); | ||
|
|
||
| maxPrices.push(currentMax.toFixed(2)); | ||
| maxStockDescriptions.push(`The highest price of ${capitaliseStockNames} in the last 5 days was ${maxPrices[i]}`) | ||
| } | ||
|
|
||
| return maxStockDescriptions; | ||
| } | ||
|
|
||
|
|
||
|
|
||
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.
Nitpick: the variable hello will be undefined, try to avoid using the word
returnfor variables and just use it for functions.