diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..44ce1027 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,13 @@ */ function getTemperatureReport(cities) { - // TODO + var temparatureInCities = []; + + for (let i = 0; i < cities.length; i++) { + let temparature = temperatureService(cities[i]) + temparatureInCities.push(`The temperature in ${cities[i]} is ${temparature} degrees`) + } + return temparatureInCities; } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..2e55218c 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,17 +5,42 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO -} + var articleTitles = []; + + for (let i = 0; i < allArticleTitles.length; i++) { + const eachTitle = allArticleTitles[i]; + if (eachTitle.length <= 65) { + articleTitles.push(eachTitle); + } + } + + return articleTitles; + } + + + /* 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) */ -function titleWithFewestWords(allArticleTitles) { - // TODO -} +function titleWithFewestWords(allArticleTitles){ + var articleTitles = [] + + var lowest = Infinity; + var articleTitleWords; + + for (var i = 0; i < allArticleTitles.length; i++) { + var words = allArticleTitles[i].split(" "); + if (words.length < lowest) { + lowest = words.length; + articleTitleWords = allArticleTitles[i]; + } + } + + return articleTitleWords; + } /* The editor of the FT has realised that headlines which have numbers in them get more clicks! @@ -23,19 +48,24 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + const regex = /\d+/; + return allArticleTitles.filter(title => regex.test(title)); } + /* 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 + const articleLength = allArticleTitles.reduce((acc, articleTitle) => acc + articleTitle.length, 0); + const averageLength = articleLength / allArticleTitles.length; + return Math.round(averageLength); } + /* ======= List of Articles - DO NOT MODIFY ===== */ const ARTICLE_TITLES = [ "Streaming wars drive media groups to spend more than $100bn on new content", diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..af5db2f0 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,8 +34,23 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO -} + + const averageStocks = closingPricesForAllStocks.length; + const lastDays = closingPricesForAllStocks[0].length; + const averages = []; + + for (let i = 0; i < averageStocks; i++) { + let sum = 0; + for (let j = 0; j < lastDays; j++) { + sum += closingPricesForAllStocks[i][j]; + } + const average = sum / lastDays; + averages.push(parseFloat(average.toFixed(2))); + } + + return averages; + } + /* We also want to see what the change in price is from the first day to the last day for each stock. @@ -48,9 +63,19 @@ 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 averageStocks = closingPricesForAllStocks.length; + const changesInPrice = []; + + for (let i = 0; i < averageStocks; i++) { + const firstDayPrice = closingPricesForAllStocks[i][0]; + const lastDayPrice = closingPricesForAllStocks[i][closingPricesForAllStocks[i].length - 1]; + const priceChange = parseFloat((lastDayPrice - firstDayPrice).toFixed(2)); + changesInPrice.push(priceChange); + } + + return changesInPrice; + } /* 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,8 +89,18 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO -} + const highestPrices = []; + + for (let i = 0; i < stocks.length; i++) { + const stockPrices = closingPricesForAllStocks[i]; + const highestPrice = Math.max(...stockPrices).toFixed(2); + const stockName = stocks[i].toUpperCase(); + highestPrices.push(`The highest price of ${stockName} in the last 5 days was ${highestPrice}`); + } + + return highestPrices; + } + /* ======= TESTS - DO NOT MODIFY ===== */