From f2f9fb27c5d10771ae20b35c1fa1fc1a207f3066 Mon Sep 17 00:00:00 2001 From: lazar-eva <103130591+lazar-eva@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:37:01 +0000 Subject: [PATCH 1/4] Ex 1 - weather report done --- 2-mandatory/1-weather-report.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..9ef4085f 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,14 +12,20 @@ */ function getTemperatureReport(cities) { - // TODO + let citiesArray = []; + cities.forEach(city => { + citiesArray.push('The temperature in '.concat(city, ' is ', + temperatureService(city), ' degrees')); + + }); + return citiesArray; } /* ======= 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 +34,7 @@ function temperatureService(city) { temparatureMap.set('Mumbai', 29); temparatureMap.set('São Paulo', 23); temparatureMap.set('Lagos', 33); - + return temparatureMap.get(city); } From b54645b2cb62a15767a4e45c3dced3eb85e2f3aa Mon Sep 17 00:00:00 2001 From: lazar-eva <103130591+lazar-eva@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:37:35 +0000 Subject: [PATCH 2/4] Ex 2 - financial times done --- 2-mandatory/2-financial-times.js | 36 +++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) 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 From 151db373c04407d7cc3988beda16749a360bcce4 Mon Sep 17 00:00:00 2001 From: lazar-eva <103130591+lazar-eva@users.noreply.github.com> Date: Mon, 6 Mar 2023 10:06:07 +0000 Subject: [PATCH 3/4] Stocks exercise done --- 2-mandatory/3-stocks.js | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) 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; } From 88d1ce11f2c46d1d7760a6e7076d816ce08ad455 Mon Sep 17 00:00:00 2001 From: lazar-eva <103130591+lazar-eva@users.noreply.github.com> Date: Mon, 6 Mar 2023 10:07:53 +0000 Subject: [PATCH 4/4] Exercise 1 weather report done --- 2-mandatory/1-weather-report.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index 9ef4085f..064a1393 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,13 +12,12 @@ */ function getTemperatureReport(cities) { - let citiesArray = []; + const statements = []; cities.forEach(city => { - citiesArray.push('The temperature in '.concat(city, ' is ', + statements.push('The temperature in '.concat(city, ' is ', temperatureService(city), ' degrees')); - }); - return citiesArray; + return statements; }