From 57b2efddb87b5d821a613dd657b23b7ae1aa2ff3 Mon Sep 17 00:00:00 2001 From: "Mr.Demir" Date: Sun, 7 May 2023 17:56:56 +0100 Subject: [PATCH] last --- 1-exercises/A-undefined/exercise.js | 6 +- 1-exercises/B-array-literals/exercise.js | 11 +- 1-exercises/C-array-get-set/exercise.js | 4 +- 1-exercises/C-array-get-set/exercises2.js | 2 + 1-exercises/D-for-loop/exercise.js | 4 + .../E-while-loop-with-array/exercise.js | 9 +- 2-mandatory/1-weather-report.js | 7 +- 2-mandatory/2-financial-times.js | 61 ++++++++-- 2-mandatory/3-stocks.js | 107 +++++++++++++++++- 9 files changed, 186 insertions(+), 25 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..ace02cfd 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,12 +12,13 @@ // Example 1 let a; console.log(a); - +//a dosen't have value // Example 2 function sayHello() { let message = "Hello"; } +// This varibale is defined only inside of function and cannot be accessed outside the function let hello = sayHello(); console.log(hello); @@ -29,8 +30,9 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// since the "sayHello()" function does not return any value, the variable "hello" is set to undefined // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// arr dosent have index 3 diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..44d55c79 100644 --- a/1-exercises/B-array-literals/exercise.js +++ b/1-exercises/B-array-literals/exercise.js @@ -5,7 +5,16 @@ */ 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 + +for (let i=1; i<= 10 ;i++){ + numbers.push(i) +} + + +// Create an array with the names of the mentors: Daniel, Irina and Rares + +let mentors = ['Daniel', 'Irina','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..9c1d01d7 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..c9e55980 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -7,6 +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..bbc6f284 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -26,7 +26,11 @@ const AGES = [ 49 ]; + // 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..451541da 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) { + let i=0 + while (i= '0' && character <= '9') { + return true; + } + } + + return false; } /* @@ -31,11 +65,14 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO -} - + let totalCharacters = 0; + for(let title of allArticleTitles) { + totalCharacters += title.length; + } + return Math.round(totalCharacters / allArticleTitles.length); +} /* ======= List of Articles - DO NOT MODIFY ===== */ const ARTICLE_TITLES = [ "Streaming wars drive media groups to spend more than $100bn on new content", diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..970ad13a 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -1,5 +1,5 @@ /* - THESE EXERCISES ARE QUITE HARD. JUST DO YOUR BEST, AND COME WITH QUESTIONS IF YOU GET STUCK :) + THESE EXERCISES ARE QUITE HARD. DON'T WORRY IF YOU CAN'T COMPLETE THEM ALL - JUST DO YOUR BEST, AND COME WITH QUESTIONS :) Imagine we a working for a finance company. Below we have: - an array of stock tickers @@ -33,8 +33,41 @@ 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! */ + +/* + SOLUTION EXPLANATION: This is a complex problem and there are many ways to solve it! + I've decided to break the large problem down into a few smaller problems. + 1. The exercise is asking us to find the average price of EACH stock. + So first, maybe we can just work out how to get the average price for a SINGLE stock. + For this, I've created a separate function called getAveragePricesForStock. + 2. We also need to work out how to round a number to 2 decimal places. + There's also a separate function for this called roundTo2Decimals. + 3. We can put these smaller solutions back together to solve the larger problem. + The top-level getAveragePrices prices function can loop through the array, + and pass each sub-array to the getAveragePricesForStock function. +*/ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averages = []; + + for(let pricesForStock of closingPricesForAllStocks) { + averages.push(getAveragePricesForStock(pricesForStock)); + } + + return averages; +} + +function getAveragePricesForStock(pricesForStock) { + let total = 0; + + for(let price of pricesForStock) { + total += price; + } + + return roundTo2Decimals(total / pricesForStock.length); +} + +function roundTo2Decimals(num) { + return Math.round(num * 100) / 100; } /* @@ -47,8 +80,26 @@ function getAveragePrices(closingPricesForAllStocks) { (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) */ + +/* + SOLUTION EXPLANATION: Again, we can break the large problem down into a few smaller problems. + 1. I've created a new function which just calculates the change in price for a SINGLE stock - getPriceChangeForStock. + 2. This new function can also use the roundTo2Decimals function we implemented earlier. + One of the many advatages of using functions is being able to re-use code! +*/ function getPriceChanges(closingPricesForAllStocks) { - // TODO + let changes = []; + + for(let pricesForStock of closingPricesForAllStocks) { + changes.push(getPriceChangeForStock(pricesForStock)) + } + + return changes; +} + +function getPriceChangeForStock(pricesForStock) { + let priceChange = pricesForStock[pricesForStock.length - 1] - pricesForStock[0] + return roundTo2Decimals(priceChange); } /* @@ -61,10 +112,54 @@ function getPriceChanges(closingPricesForAllStocks) { For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33" The test will check for this exact string. The stock ticker should be capitalised. - The price should be shown with exactly 2 decimal places. + The price should be shown with EXACTLY 2 decimal places. +*/ + +/* + SOLUTION EXPLANATION: We can also break this problem down into a smaller problems. + 1. I've created a new function which just calculates the highest price for a SINGLE stock - getHighestPrice. + 2. This new function can also use the roundTo2Decimals function we implemented earlier. + ALTERNATE SOLUTION: See highestPriceDescriptionsAlternate + I've included an alternate solution here which makes use of JavaScript's Math.max() function + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max + The Math.max() function takes zero or more numbers as input parameters. + We can convert our array into individual numbers that can then be passed into this function using the spread syntax: ... + Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let descriptions = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++) { + let highestPrice = getHighestPrice(closingPricesForAllStocks[i]); + descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`); + } + + return descriptions; +} + +function getHighestPrice(pricesForStock) { + // initialising to 0, as we're expecting this value to be overriden by the first price in the array + let highestPriceSoFar = 0; + + for(let price of pricesForStock) { + // if this price is higher than the highest price we've seen so far, it becomes the new highest price + if(price > highestPriceSoFar) { + highestPriceSoFar = price; + } + } + + return highestPriceSoFar; +} + +function highestPriceDescriptionsAlternate(closingPricesForAllStocks, stocks) { + let descriptions = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++) { + let highestPrice = Math.max(...closingPricesForAllStocks[i]); + descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`); + } + + return descriptions; } @@ -91,4 +186,4 @@ test("should return a description of the highest price for each stock", () => { "The highest price of TSLA in the last 5 days was 1101.30" ] ); -}); +}); \ No newline at end of file