From c7628f0e2a26ed6e731a4862087eab8a3a5d496e Mon Sep 17 00:00:00 2001 From: Monika39-maker Date: Fri, 14 Jan 2022 11:08:51 +0000 Subject: [PATCH 1/2] initial commit --- 1-exercises/A-undefined/exercise.js | 5 +++ 1-exercises/B-while-loop/exercise.js | 21 +++++++--- .../C-while-loop-with-array/exercise.js | 6 ++- 1-exercises/D-do-while/exercise.js | 13 ++++++- 1-exercises/E-for-loop/exercise1.js | 5 +-- 1-exercises/E-for-loop/exercise2.js | 5 +++ 1-exercises/F-for-of-loop/exercise.js | 7 ++++ 2-mandatory/1-weather-report.js | 8 +++- 2-mandatory/2-retrying-random-numbers.js | 8 +++- 2-mandatory/3-financial-times.js | 34 +++++++++++++++-- 2-mandatory/4-stocks.js | 38 +++++++++++++++++-- 3-extra/1-factorial.js | 9 ++++- app.js | 31 +++++++++++++++ mytest.js | 12 ++++++ 14 files changed, 182 insertions(+), 20 deletions(-) create mode 100644 app.js create mode 100644 mytest.js diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..1d1b993c 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -13,14 +13,17 @@ let a; console.log(a); +//Answer: Because a has not been assigned any value // Example 2 function sayHello() { let message = "Hello"; } + let hello = sayHello(); console.log(hello); +//Answer: Because the function is not returning anything // Example 3 @@ -29,8 +32,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); +//Answer: Because no arguement has been passed inside the function when calling it // Example 4 let arr = [1,2,3]; console.log(arr[3]); +//Answer: Because the arr has 3 elements in it. So arr[3] means 4th element which is not present in the arr diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..d2867ca4 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,20 @@ */ function evenNumbers(n) { - // TODO + let i = 0; + let evenNumberArray = [] + while (evenNumberArray.length < n) { + if (i%2 === 0) { + let strNum = i.toString() + evenNumberArray.push(strNum); + } + + i++ + } + + return evenNumberArray.join(',') + } - -evenNumbers(3); // should output 0,2,4 -evenNumbers(0); // should output nothing -evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18 +console.log(evenNumbers(3)); // should output 0,2,4( +console.log(evenNumbers(0)); // should output nothing +console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..c7462ef8 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,11 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + for (let i = 0; i< birthdays.length; i++) { + if (birthdays[i].includes("July")) { + return birthdays[i] + } + } } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..da5fefd8 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,18 @@ */ function evenNumbersSum(n) { - // TODO + let currentNum = 0; + let evenNumArr = []; + + do { + if (currentNum%2 === 0 ) { + evenNumArr.push(currentNum) + + } + currentNum += 1; + + } while (evenNumArr.length < n) + return evenNumArr.reduce((prevNum, num) => {return prevNum+num}) } console.log(evenNumbersSum(3)); // should output 6 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..3512b663 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,8 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { + +for (let i=0; i < 26; i++) { console.log(String.fromCharCode(97 + i)); - i++; } // The output shouldn't change. diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..1c8544a1 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -28,6 +28,11 @@ const AGES = [ // TODO - Write for loop code here + +for (let writerIndex = 0 ; writerIndex {return prevTitleChar + currTitleChar}); + const avgNumOfChar = (totalOfChar/numOfCharArr.length).toFixed() + return Number(avgNumOfChar) } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..7dfe74b8 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let avgPricesArr = []; + + for (let prices of closingPricesForAllStocks) { + let totalPrices = prices.reduce((prevPrice, currPrice) => { return (prevPrice + currPrice)}) + let avgPrices = (totalPrices/5).toFixed(2) + avgPricesArr.push(Number(avgPrices)) + } + return avgPricesArr } /* @@ -48,7 +55,15 @@ 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 priceDiffArr = []; + + + for (let price of closingPricesForAllStocks) { + + let priceDiff = (price[price.length - 1] - price[0]).toFixed(2) + priceDiffArr.push(Number(priceDiff)) + } + return priceDiffArr } /* @@ -64,7 +79,24 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let highestPriceDescriptionArr = []; + + + + function compareFunction(a, b) { + return b-a + } + + + for (let currStock = 0; currStock < closingPricesForAllStocks.length; currStock++) { + const sortedPrice = closingPricesForAllStocks[currStock].sort(compareFunction) + const highestPrice = sortedPrice[0].toFixed(2); + const highestPriceInt = Number(highestPrice).toFixed(2) + const highestPriceDescription = `The highest price of ${stocks[currStock].toUpperCase()} in the last 5 days was ${highestPriceInt}` + highestPriceDescriptionArr.push(highestPriceDescription) + + } + return highestPriceDescriptionArr } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..d720dbaa 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,14 @@ */ function factorial(input) { - // TODO + let numReducedByOneArr = [] + while (input !== 1) { + let newNum = input + numReducedByOneArr.push(newNum) + input-- + } + const multipleOfAll = numReducedByOneArr.reduce((prev, curr) => {return prev * curr }) + return multipleOfAll } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/app.js b/app.js new file mode 100644 index 00000000..faf78ac9 --- /dev/null +++ b/app.js @@ -0,0 +1,31 @@ +const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; + +const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ + [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL + [340.69, 342.45, 334.69, 333.20, 327.29], // MSFT + [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN + [2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL + [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA +]; + +function highestPriceDescriptions(closingPricesForAllStocks, stocks) { + let highestPriceDescriptionArr = []; + + + + function compareFunction(a, b) { + return b-a + } + + + for (let currStock = 0; currStock < closingPricesForAllStocks.length; currStock++) { + const sortedPrice = closingPricesForAllStocks[currStock].sort(compareFunction) + const highestPrice = (Number(sortedPrice[0]).toFixed(2)) + const highestPriceDescription = `The highest price of ${stocks[currStock].toUpperCase()} in the last 5 days was ${highestPrice}` + highestPriceDescriptionArr.push(highestPriceDescription) + + } + return highestPriceDescriptionArr +} + +console.log(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)) \ No newline at end of file diff --git a/mytest.js b/mytest.js new file mode 100644 index 00000000..ec0dc55b --- /dev/null +++ b/mytest.js @@ -0,0 +1,12 @@ +function factorial(input) { + let numReducedByOneArr = [] + while (input !== 1) { + let newNum = input + numReducedByOneArr.push(newNum) + input-- + } + const multipleOfAll = numReducedByOneArr.reduce((prev, curr) => {return prev * curr }) + return multipleOfAll +} + +console.log(factorial(5)) \ No newline at end of file From 06c5fd6d6147e6b5e9ee14de0c91b0e2438729a5 Mon Sep 17 00:00:00 2001 From: Monika39-maker Date: Wed, 2 Feb 2022 12:28:21 +0000 Subject: [PATCH 2/2] finished extra assignments --- 3-extra/2-array-of-objects.js | 45 +++++++++++++- 3-extra/3-fibonacci.js | 10 ++- app.js | 114 +++++++++++++++++++++++++++------- mytest.js | 23 +++---- 4 files changed, 157 insertions(+), 35 deletions(-) diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..0d098c57 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -10,8 +10,51 @@ Each title in the resulting array should be the highest rated book in its genre. */ +let higherRatedBook = [] +let higherRate = 0; +let genreArray = [] + +// function to collect all the unique genres in the object and add it to the genreArray +function createGenreArray(availableBooks) { + + for (let everyBook of availableBooks) { + if (!genreArray.includes(everyBook.genre)) { + genreArray.push(everyBook.genre) + } + } + return genreArray +} + function getHighestRatedInEachGenre(books) { - // TODO + const allAvailableGenres = createGenreArray(books) + + + + // let highestRating; + let highestRatedBooksArr = []; + + for (let i = 0; i {return prev * curr }) - return multipleOfAll +function generateFibonacciSequence(n) { + let fibonacciNumArr = [0, 1]; + + for (let i=2; i