diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..5bec732c 100644 --- a/1-exercises/B-array-literals/exercise.js +++ b/1-exercises/B-array-literals/exercise.js @@ -4,8 +4,8 @@ Declare some variables assigned to arrays of values */ -let numbers = []; // add numbers from 1 to 10 into this array -let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares +let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // add numbers from 1 to 10 into this array +let mentors = ["Daniel", "Irina", "Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/C-array-get-set/exercise.js b/1-exercises/C-array-get-set/exercise.js index 5ca911d5..3e1fc029 100644 --- a/1-exercises/C-array-get-set/exercise.js +++ b/1-exercises/C-array-get-set/exercise.js @@ -5,11 +5,11 @@ */ function first(arr) { - return; // complete this statement + return numbers=[0]; // complete this statement } function last(arr) { - return; // complete this statement + return names=[2]; // complete this statement } /* diff --git a/1-exercises/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index d584cd75..1b6f66a3 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -17,7 +17,14 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let currentIndex = 0; + + while(currentIndex < birthdays.length){ + if (birthdays[currentIndex].includes("July")){ + return birthdays[currentIndex]; + } + currentIndex ++; + } } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..53446cb6 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,15 @@ */ function getTemperatureReport(cities) { - // TODO + output=[]; + for (city of cities) { + temperature = temperatureService(city); + string = `The temperature in ${city} is ${temperature} degrees`; + output.push (string); + + } + return output; + } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..d3a07bfa 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,7 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + allTitles = []; + for (headline of allArticleTitles){ + if (headline.length <= 65) { + allTitles.push(headline) + } + } + return allTitles; } /* @@ -14,24 +20,58 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + + let shortestLine; + let length_headline = 100000; + + for (headline of allArticleTitles) { + let currentTitleWordCount = headline.split (" ").length; + + if (currentTitleWordCount < length_headline) { + shortestLine = headline; + length_headline = currentTitleWordCount; + } + } + + return shortestLine; + } + + /* The editor of the FT has realised that headlines which have numbers in them get more clicks! 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 hasNumber(myString) { + return /\d/.test(myString); +} + function headlinesWithNumbers(allArticleTitles) { - // TODO + let arrayWithNumbers = []; + for (headline of allArticleTitles) { + let includesNumber = hasNumber(headline) + if (includesNumber) { + arrayWithNumbers.push(headline) + } + } + + return arrayWithNumbers; + } -/* +/* 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 totalCharacters = 0; + for (const headline of allArticleTitles){ + totalCharacters = totalCharacters + headline.length; + } + return Math.round(totalCharacters / allArticleTitles.length); } diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..3c454909 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let arrayOfAveragePrices = []; + + for (const stockPricesLastFiveDays of closingPricesForAllStocks) { + let sum = 0; + for (const stock of stockPricesLastFiveDays){ + sum += stock; + } + const averagePrice = Math.round((sum / stockPricesLastFiveDays.length ) * 100 ) / 100 + + arrayOfAveragePrices.push(averagePrice) + } + return arrayOfAveragePrices; } /* @@ -48,9 +59,20 @@ 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 newArrayOfDifference = []; + for (let oneSetOfPrices of closingPricesForAllStocks){ + lengthOfArray = oneSetOfPrices.length; + finalPositionInArray = lengthOfArray - 1; + + let firstPrice = oneSetOfPrices[0]; + let lastPrice = oneSetOfPrices[finalPositionInArray]; + let differencePrice = Math.round((lastPrice - firstPrice) * 100) / 100; + newArrayOfDifference.push(differencePrice); + } + return newArrayOfDifference; } + /* 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 +86,19 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let highestPriceForEachStock = []; + let highestPrice = 0; + let stock = 0; + for (element of closingPricesForAllStocks) { + highestPrice = Math.max(...element).toFixed(2); + highestPriceForEachStock.push( `The highest price of ${stocks[stock].toUpperCase()} in the last 5 days was ${highestPrice}`); + stock++; + } + return highestPriceForEachStock; } + + + /* ======= TESTS - DO NOT MODIFY ===== */