diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..0acd0289 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,15 @@ */ function getTemperatureReport(cities) { - // TODO + const citiesReport = []; + + for (const city of cities){ + let cityTemparature = temperatureService(city); + let statement = `The temperature in ${city} is ${cityTemparature} degrees`; + + citiesReport.push(statement); + } + return citiesReport; } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..81d91f58 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,7 +5,15 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + + let fittingArticles = []; + + for (const title of allArticleTitles) { + if (title.length <= 65){ + fittingArticles.push(title); + } + } + return fittingArticles; } /* @@ -14,7 +22,13 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let shortestTitle = allArticleTitles[0]; + for (const newTitle of allArticleTitles){ + if (newTitle.length <= shortestTitle.length){ + shortestTitle = newTitle; + } + } + return shortestTitle; } /* @@ -23,7 +37,18 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + let clickables = []; + + for (const title of allArticleTitles){ + for (const character of title){ + if ('0123456789'.includes(character)){ //if (character = Number) my previous -failed- solution. + clickables.push(title); + break; + } + + } + } + return clickables; } /* @@ -31,9 +56,22 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO -} + let charactersCount= 0; + let titlesCount = 0; + for (const title of allArticleTitles){ + + if (title.length > 0){ + charactersCount += title.length; + + } + + titlesCount++; + } + + let average = charactersCount / titlesCount; + return Math.round(average); +} /* ======= List of Articles - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..71b36ce6 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,7 +34,22 @@ 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 +63,13 @@ 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 +84,32 @@ function getPriceChanges(closingPricesForAllStocks) { The stock ticker should be capitalised. The price should be shown with exactly 2 decimal places. */ + function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + const highestPriceReports = []; + let pricesArray = 0; + + + for (const stock of stocks){ + let highestPrice = 0; + let stockPrices = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[pricesArray]; + + for (const price of stockPrices){ + + if (price > highestPrice){ + highestPrice = price; + } + + } + + // highestPrice = Number(highestPrice.toFixed(2)); + //(Number object switchh the value from "string" to numbers, which don't see the onely zero at the decimal place as a number then). + highestPriceReports.push(`The highest price of ${stock.toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`); + pricesArray++; + + } + + return highestPriceReports; }