From cc3eab744937b81771aa0b10eefd0fff15386749 Mon Sep 17 00:00:00 2001 From: ElenaBarker <113217019+ElenaBarker@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:56:08 +0000 Subject: [PATCH 1/3] mandatory exercises weather report and financial times done --- 1-exercises/B-array-literals/exercise.js | 4 +-- 1-exercises/C-array-get-set/exercise.js | 4 +-- 2-mandatory/1-weather-report.js | 10 +++++- 2-mandatory/2-financial-times.js | 44 +++++++++++++++++++++--- 4 files changed, 53 insertions(+), 9 deletions(-) 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/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..9f00615e 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.lenght <= 65) { + allTitles.push(headline) + } + } + return allTitles; } /* @@ -14,8 +20,24 @@ 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! @@ -23,7 +45,17 @@ 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 arrayWithNumbers = []; + for (headline of allArticleTitles) { + for (component of headline) { + let component = component(parseInt) + if (component.includes(component)) { + arrayWithNumbers.push(headline) + } + } + } + return arrayWithNumbers; + } /* @@ -31,7 +63,11 @@ function headlinesWithNumbers(allArticleTitles) { 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); } From 119820b0c585dab61348a34d5ba767b02b93adca Mon Sep 17 00:00:00 2001 From: ElenaBarker <113217019+ElenaBarker@users.noreply.github.com> Date: Sat, 11 Mar 2023 12:32:42 +0000 Subject: [PATCH 2/3] financial times task finished with all tests passed --- 2-mandatory/2-financial-times.js | 20 ++++++++++++-------- 2-mandatory/3-stocks.js | 6 +++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 9f00615e..d3a07bfa 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -7,7 +7,7 @@ function potentialHeadlines(allArticleTitles) { allTitles = []; for (headline of allArticleTitles){ - if (headline.lenght <= 65) { + if (headline.length <= 65) { allTitles.push(headline) } } @@ -44,21 +44,25 @@ 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 hasNumber(myString) { + return /\d/.test(myString); +} + function headlinesWithNumbers(allArticleTitles) { - let arrayWithNumbers = []; + let arrayWithNumbers = []; for (headline of allArticleTitles) { - for (component of headline) { - let component = component(parseInt) - if (component.includes(component)) { - arrayWithNumbers.push(headline) - } + 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. */ diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..25d53264 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,7 +34,11 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + const arrayOfAveragePrices = []; + for (const element of closingPricesForAllStocks) { + + } + } /* From 39d7f4ae8dd062aa79086fb671b4af0ab44f1e82 Mon Sep 17 00:00:00 2001 From: ElenaBarker <113217019+ElenaBarker@users.noreply.github.com> Date: Thu, 16 Mar 2023 21:19:25 +0000 Subject: [PATCH 3/3] Mandatory exercises all done --- .../E-while-loop-with-array/exercise.js | 9 +++- 2-mandatory/3-stocks.js | 41 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) 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/3-stocks.js b/2-mandatory/3-stocks.js index 25d53264..3c454909 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,11 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - const arrayOfAveragePrices = []; - for (const element of closingPricesForAllStocks) { - + 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; } /* @@ -52,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 @@ -68,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 ===== */