diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..064a1393 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,14 +12,19 @@ */ function getTemperatureReport(cities) { - // TODO + const statements = []; + cities.forEach(city => { + statements.push('The temperature in '.concat(city, ' is ', + temperatureService(city), ' degrees')); + }); + return statements; } /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); + let temparatureMap = new Map(); temparatureMap.set('London', 10); temparatureMap.set('Paris', 12); @@ -28,7 +33,7 @@ function temperatureService(city) { temparatureMap.set('Mumbai', 29); temparatureMap.set('São Paulo', 23); temparatureMap.set('Lagos', 33); - + return temparatureMap.get(city); } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..fe95bc94 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,7 +5,10 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + const smallArticle = allArticleTitles.filter(article => { + return article.length < 65; + }) + return smallArticle; } /* @@ -14,7 +17,13 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let fewestWordTitle = allArticleTitles[0]; + for (const article of allArticleTitles) { + if (article.length < fewestWordTitle.length) { + fewestWordTitle = article; + } + } + return fewestWordTitle; } /* @@ -22,16 +31,29 @@ function titleWithFewestWords(allArticleTitles) { 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 articleWithNums = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (/\d/.test(allArticleTitles[i])) { + articleWithNums.push(allArticleTitles[i]); + } + } + return articleWithNums; + } + /* 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 sumOfAllChars = 0; + allArticleTitles.forEach(article => { + sumOfAllChars += article.length; + }); + return Math.round(sumOfAllChars / allArticleTitles.length); } @@ -78,4 +100,4 @@ test("should only return headlines containing numbers", () => { test("should return the average number of characters in a headline", () => { expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); -}); +}); \ No newline at end of file diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..7c5543e4 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -33,8 +33,28 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Solve the smaller problems, and then build those solutions back up to solve the larger problem. Functions can help with this! */ +// function getAveragePrices(closingPricesForAllStocks) { +// const averagePrices = closingPricesForAllStocks.map(stock => { +// const sumOfStocks = stock.reduce((accumulator, currentValue) => { + +// return accumulator + currentValue; +// }) +// return Math.round((sumOfStocks / stock.length) * 100) / 100; +// }) +// return averagePrices; +// } + function getAveragePrices(closingPricesForAllStocks) { - // TODO + const averagePrices = []; + for (const closingPrices of closingPricesForAllStocks) { + let sum = 0; + + for (const closingPrice of closingPrices) { + sum += closingPrice; + } + averagePrices.push(Math.round(sum / closingPrices.length * 100) / 100); + } + return averagePrices; } /* @@ -48,7 +68,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 priceChange = closingPricesForAllStocks.map(stock => { + const change = (stock[stock.length - 1] - stock[0]); + return Math.round(change * 100) / 100; + + }) + return priceChange; } /* @@ -64,7 +89,13 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + const descriptions = []; + for (let index = 0; index < closingPricesForAllStocks.length; index++) { + const closingPrices = closingPricesForAllStocks[index]; + const maxPrice = Math.max(...closingPrices).toFixed(2); + descriptions.push(`The highest price of ${stocks[index].toUpperCase()} in the last 5 days was ${maxPrice}`); + } + return descriptions; }