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/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