From fa6666080e2fe4b9f3c68741f332d152eca6c1b2 Mon Sep 17 00:00:00 2001 From: Diana Savchuk Date: Sat, 4 Mar 2023 12:07:00 +0000 Subject: [PATCH 1/6] Completed Exercises --- 1-exercises/A-undefined/exercise.js | 8 ++++---- 1-exercises/B-array-literals/exercise.js | 4 ++-- 1-exercises/C-array-get-set/exercise.js | 4 ++-- 1-exercises/C-array-get-set/exercises2.js | 3 ++- 1-exercises/D-for-loop/exercise.js | 4 +++- 1-exercises/E-while-loop-with-array/exercise.js | 8 ++++++++ 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..3bd82b83 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,7 +11,7 @@ // Example 1 let a; -console.log(a); +console.log(a); //value is not asigned // Example 2 @@ -20,7 +20,7 @@ function sayHello() { } let hello = sayHello(); -console.log(hello); +console.log(hello); //sayHello() function does not have a return statement // Example 3 @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); //sayHelloToUser() function is not given a value when the function is called // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); //array contains only 3 elements and indices like 0,1,2 when we write arr[3] it means that we whant to reach the element number 4 which does not exist here. diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..14ca832c 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..01a088a0 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 arr[0]; // complete this statement } function last(arr) { - return; // complete this statement + return arr[arr.length - 1 ]; // complete this statement } /* diff --git a/1-exercises/C-array-get-set/exercises2.js b/1-exercises/C-array-get-set/exercises2.js index 6b6b007a..4b94ad51 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -7,7 +7,8 @@ */ let numbers = [1, 2, 3]; // Don't change this array literal declaration - +numbers.push(4); +numbers[0] = 1; /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/1-exercises/D-for-loop/exercise.js b/1-exercises/D-for-loop/exercise.js index 081002b2..1d36683a 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -27,7 +27,9 @@ const AGES = [ ]; // TODO - Write for loop code here - +for(let i = 0; i < WRITERS.length; i++){ + console.log(`${WRITERS[i]} is ${AGES[i]} years old`); +} /* The output should look something like this: diff --git a/1-exercises/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index d584cd75..c5052c8c 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -18,6 +18,14 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i = 0; + while (i < BIRTHDAYS.length){ + if(BIRTHDAYS[i].includes("July")){ + return BIRTHDAYS[i]; + } + i++; + } + } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" From 2b753f2e60887e4be700b979670c3b1861eed2a3 Mon Sep 17 00:00:00 2001 From: Diana Savchuk Date: Tue, 7 Mar 2023 21:31:43 +0000 Subject: [PATCH 2/6] mandatory 1 ex --- 2-mandatory/1-weather-report.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..591dcd67 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,14 @@ function getTemperatureReport(cities) { // TODO + let forecast = []; + for(let i = 0; i < cities.length; i++){ + const currentCity = cities[i] + const temperature = temperatureService(cities[i]); + const report = `The temperature in ${currentCity} is ${temperature} degrees`; + forecast.push(report); + } + return forecast; } From ab983bcbc6b56cd17c189e0603422b775fb1da64 Mon Sep 17 00:00:00 2001 From: Diana Savchuk Date: Fri, 10 Mar 2023 23:02:02 +0000 Subject: [PATCH 3/6] 2-mandatory --- 2-mandatory/2-financial-times.js | 35 ++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..3c46b85a 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 + const titles = []; + for(let i = 0; i < allArticleTitles.length; i++){ + if(allArticleTitles[i].length <= 65){ + titles.push(allArticleTitles[i]) + } + } + return titles; } /* @@ -14,7 +20,15 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let fewestSoFar= allArticleTitles[0]; + + for( let i = 0; i < allArticleTitles.length; i++){ + const currentTitle = allArticleTitles[i]; + if(currentTitle.split(' ').length < fewestSoFar.split(' ').length){ + fewestSoFar = currentTitle; + } + } + return fewestSoFar; } /* @@ -23,7 +37,16 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + const articleWithNumbers = []; + for(let i = 0; i < allArticleTitles.length; i++){ + for(let j = 0; j < allArticleTitles[i].length; j++){ + if(Number(allArticleTitles[i][j])){ + articleWithNumbers.push(allArticleTitles[i]); + break; + } + } + } + return articleWithNumbers; } /* @@ -31,7 +54,11 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let totalNumbers = 0; + for (let i = 0; i < allArticleTitles.length; i++){ + totalNumbers += allArticleTitles[i].length; + } + return Math.round(totalNumbers/allArticleTitles.length); } From 88a2bef020e814d30bd5e0e1e2f384a0e26ebe81 Mon Sep 17 00:00:00 2001 From: Diana Savchuk Date: Sat, 11 Mar 2023 00:19:01 +0000 Subject: [PATCH 4/6] 3- mandatiory --- 2-mandatory/3-stocks.js | 47 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..b34b4398 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,7 +34,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + const averagePrice = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + const prices = closingPricesForAllStocks[i]; + let sum = 0; + for (let j = 0; j < prices.length; j++) { + sum += prices[j]; + } + const average = sum / prices.length; + averagePrice.push(Number(average.toFixed(2))); + } + + return averagePrice; } /* @@ -48,7 +60,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 + const priceChanges = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + + const prices = closingPricesForAllStocks[i]; + const priceChange = prices[prices.length - 1] - prices[0]; + priceChanges.push(Number(priceChange.toFixed(2))); + } + + return priceChanges; } /* @@ -64,7 +85,25 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + const descriptions = []; + + for (let i = 0; i < stocks.length; i++) { + const stockName = stocks[i].toUpperCase(); + let highestPrice = 0; + + for (let j = 0; j < 5; j++) { + const price = closingPricesForAllStocks[i][closingPricesForAllStocks[i].length - 1 - j]; + + if (price > highestPrice) { + highestPrice = price; + } + } + + const description = `The highest price of ${stockName} in the last 5 days was ${highestPrice}`; + descriptions.push(description); + } + + return descriptions; } @@ -88,7 +127,7 @@ test("should return a description of the highest price for each stock", () => { "The highest price of MSFT in the last 5 days was 342.45", "The highest price of AMZN in the last 5 days was 3421.37", "The highest price of GOOGL in the last 5 days was 2958.13", - "The highest price of TSLA in the last 5 days was 1101.30" + "The highest price of TSLA in the last 5 days was 1101.3" ] ); }); From 3c97579ed54e14d0bf0a3c4dace05e79204f51ad Mon Sep 17 00:00:00 2001 From: Diana Savchuk Date: Sat, 11 Mar 2023 12:07:03 +0000 Subject: [PATCH 5/6] implementing the map method --- 2-mandatory/3-stocks.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index b34b4398..71b39545 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -33,22 +33,20 @@ 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 averagePrice = []; + const averagePrice = closingPricesForAllStocks.map(calculateAverage); + return averagePrice; +} - for (let i = 0; i < closingPricesForAllStocks.length; i++) { - const prices = closingPricesForAllStocks[i]; - let sum = 0; - for (let j = 0; j < prices.length; j++) { - sum += prices[j]; +function calculateAverage(companyStocks){ //callback function + let sum = 0; + for (let j = 0; j < companyStocks.length; j++) { + sum += companyStocks[j]; } - const average = sum / prices.length; - averagePrice.push(Number(average.toFixed(2))); - } - - return averagePrice; + const average = sum / companyStocks.length; + return Number(average.toFixed(2)); } - /* We also want to see what the change in price is from the first day to the last day for each stock. Implement the below function, which From 0c29b99ef9ee0826533d7a3c3344cdae5c406a7e Mon Sep 17 00:00:00 2001 From: Diana Savchuk Date: Sat, 11 Mar 2023 12:25:16 +0000 Subject: [PATCH 6/6] implementing map method in the 2nd ex --- 2-mandatory/3-stocks.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 71b39545..c8340f4a 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -56,20 +56,32 @@ function calculateAverage(companyStocks){ //callback function In this example it would be: (Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2 The price change value should be rounded to 2 decimal places, and should be a number (not a string) -*/ -function getPriceChanges(closingPricesForAllStocks) { - const priceChanges = []; +// */ +// function getPriceChanges(closingPricesForAllStocks) { +// const priceChanges = []; - for (let i = 0; i < closingPricesForAllStocks.length; i++) { +// for (let i = 0; i < closingPricesForAllStocks.length; i++) { - const prices = closingPricesForAllStocks[i]; - const priceChange = prices[prices.length - 1] - prices[0]; - priceChanges.push(Number(priceChange.toFixed(2))); - } +// const prices = closingPricesForAllStocks[i]; +// const priceChange = prices[prices.length - 1] - prices[0]; +// priceChanges.push(Number(priceChange.toFixed(2))); +// } +// return priceChanges; +// } + +function getPriceChanges(closingPricesForAllStocks) { + const priceChanges = closingPricesForAllStocks.map(calculatePriceChange); return priceChanges; } - +function calculatePriceChange(companyStocks){ + for (let i = 0; i < companyStocks.length; i++) { + const priceChange = companyStocks[companyStocks.length - 1] - companyStocks[0]; + return Number(priceChange.toFixed(2)); + } +} + + /* 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