diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..19ed6c36 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,9 +12,17 @@ */ function getTemperatureReport(cities) { - // TODO + let report = []; + for (let city of cities) { + let temparature = temperatureService(city); + report.push( `The temperature in ${city} is ${temparature} degrees`); + + + + } + return report; } - + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..4eda6f63 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,7 +5,19 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + // TODO l + let headlines=[]; + + + + for(let title of allArticleTitles) { + if(title.length <= 65) { + headlines.push(title); + } + } + + + return headlines; } /* @@ -14,6 +26,18 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { + let result = allArticleTitles[0]; + let resultlengh = result.split(' ').length; + for ( let i=1; i< allArticleTitles.length; i++ ){ + + let words = allArticleTitles[i].split(' '); + if (resultlengh > words.length){ + result= allArticleTitles[i] + resultlengh = words.length + + } + } + return result; // TODO } @@ -23,6 +47,14 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { + + let headlines = []; + for (let title of allArticleTitles){ + if (/\d/.test(title)){ + headlines.push(title); + } + } + return headlines; // TODO } @@ -31,8 +63,13 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + totalNumbers = 0; + for (let i = 0; i < allArticleTitles.length; i++){ + totalNumbers += allArticleTitles[i].length; + } + return Math.round(totalNumbers/allArticleTitles.length); } + diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..0fddd381 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,8 +34,36 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + + let averagePrices = []; + for (let stockPrices of closingPricesForAllStocks){ + let sumOfPrices = 0; + for (let price of stockPrices) { + sumOfPrices = sumOfPrices + price; + } + let averagePrice = sumOfPrices / stockPrices.length; + let roundedAverage = Math.round(averagePrice * 100) / 100; + averagePrices.push(roundedAverage); + } + + + + return averagePrices; } + // TODO + + /* let averagePrice = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++){ + let sum = 0; + for (let j = 0; j < closingPricesForAllStocks[i].lenght; j++){ + sum += closingPricesForAllStocks[i][j]; + } + averagePrice.push(Number((sum / closingPricesForAllStocks[i].lenght).toFixed(2))); + + } + return averagePrice; */ + + /* We also want to see what the change in price is from the first day to the last day for each stock. @@ -48,7 +76,16 @@ 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 + let priceChanges = []; + + for( let stockPrices of closingPricesForAllStocks){ + let startPrice = stockPrices[0] * 100; + let closePrice = stockPrices[stockPrices.length - 1] * 100; + priceChanges.push(math.round(closePrice - startPrice) / 100); + } + + + return priceChanges; } /* @@ -64,12 +101,32 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let highestPriceStrings = []; + + + for(let i = 0; i< stocks.length; i++){ + let stockName = stocks[i].toUpperCase(); + let stockPrices = closingPricesForAllStocks[i]; + + let highestPrice; + for (let price of stockPrices){ + if (highestPrice == undefined || price > highestPrice){ + highestPrice = price; + } + } + highestPriceStrings.push(`The highest price of ${"stockName"} in the last 5 days was ${highestPrice.toFixed(2)}`); + } + + + + + + return highestPriceStrings; } /* ======= TESTS - DO NOT MODIFY ===== */ -test("should return the average price for each stock", () => { +test.only("should return the average price for each stock", () => { expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( [176.89, 335.66, 3405.66, 2929.22, 1041.93] );