diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..e01ddb89 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -11,8 +11,15 @@ - Hint: you can call the temperatureService function from your function */ -function getTemperatureReport(cities) { - // TODO + +function getTemperatureReport(cities ) { + const statements = []; + for ( const city of cities){ + let degrees = temperatureService (city); + let statement = ("The temperature in " + city + " is " + degrees + " degrees"); + statements.push(statement); +} +return statements; } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..ccc5028f 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,7 +5,14 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + const shortArticleTitles=[ ]; + for ( const articleTitle of allArticleTitles) { + if (articleTitle.length <= 65) { + shortArticleTitles.push(articleTitle); + } + } + return shortArticleTitles; + } /* @@ -14,16 +21,37 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + const separateTitleArray = []; + for (const articleTitle of allArticleTitles) { + allArticleTitles.toString().split(" "); + separateTitleArray.push(articleTitle) + } + let titleWithFewestWords = separateTitleArray.reduce((fewestWords, currentWords) => { + return currentWords.length < fewestWords.length ? currentWords : fewestWords; + }, separateTitleArray[0]); + return titleWithFewestWords; } + /* 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 + function headlinesWithNumbers(allArticleTitles) { + let section = []; + + for (const title of allArticleTitles){ + for (const character of title){ + if ('0123456789'.includes(character)){ + section.push(title); + break; + } + + } + } + return section; + } /* @@ -31,8 +59,13 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO -} + let totalCharacters = 0; + for (const headline of allArticleTitles){ + totalCharacters = totalCharacters + headline.length; + } + return Math.round(totalCharacters / allArticleTitles.length); +} + diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..3f9499ca 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,7 +34,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averagePrices = []; + for (const stock of CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS){ + let pricesTotal = 0; + for (const prices of stock){ + pricesTotal += prices; + } + let averagePrice = Number((pricesTotal / stock.length).toFixed(2)); + averagePrices.push(averagePrice); + } + + return averagePrices; } /* @@ -48,7 +58,12 @@ 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 + const priceChanges = []; + for (const stock of CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS){ + let priceChange = Number((stock[4] - stock[0]).toFixed(2)); + priceChanges.push(priceChange); + } + return priceChanges } /* @@ -63,8 +78,22 @@ function getPriceChanges(closingPricesForAllStocks) { The stock ticker should be capitalised. The price should be shown with exactly 2 decimal places. */ -function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO +function highestPriceDescriptions(closingPricesForAllStocks , stocks) { + const highestPriceReports = []; + for (let i=0 ; i < closingPricesForAllStocks.length ; i++){ + let highestPrice = 0 + + closingPricesForAllStocks[i].forEach(prices => { + if (prices > highestPrice){ + highestPrice=prices + } + + }); + + highestPrice = Number(highestPrice) + highestPriceReports.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`) + } + return highestPriceReports }