diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..974fdd23 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,27 +10,25 @@ */ // Example 1 -let a; +let a = 1; console.log(a); - // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; + return message; } let hello = sayHello(); console.log(hello); - // Example 3 function sayHelloToUser(user) { - console.log(`Hello ${user}`); + console.log(`Hello ${user}`); } -sayHelloToUser(); - +sayHelloToUser("some value"); // Example 4 -let arr = [1,2,3]; -console.log(arr[3]); +let arr = [1, 2, 3]; +console.log(arr); diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..c00b78fc 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -5,10 +5,33 @@ The list of numbers should start with 0. n is being passed in as a parameter. */ +// function evenNumbers(n) { +// // TODO + +// while(n ){ +// console.log(); +// } +// } + +//experiment + function evenNumbers(n) { - // TODO + // TODO + let numArray = [0,1,2,3,4,5,6,7,8,9]; + let counter = 0; + let stringVal = []; + + while (counter { - expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [176.89, 335.66, 3405.66, 2929.22, 1041.93] - ); + expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([ + 176.89, 335.66, 3405.66, 2929.22, 1041.93, + ]); }); test("should return the price change for each stock", () => { - expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [-6.2, -13.4, 23.9, -82.43, -162.77] - ); + expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([ + -6.2, -13.4, 23.9, -82.43, -162.77, + ]); }); test("should return a description of the highest price for each stock", () => { - expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual( - [ - "The highest price of AAPL in the last 5 days was 180.33", - "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" - ] - ); + expect( + highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS) + ).toEqual([ + "The highest price of AAPL in the last 5 days was 180.33", + "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", + ]); }); diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..c5e8c2e8 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,6 +10,11 @@ function factorial(input) { // TODO + let factorial = 1; + for (let i = input; i > 0; i--) { + factorial *= i; + } + return factorial; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..8d99d600 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,72 +11,107 @@ */ function getHighestRatedInEachGenre(books) { - // TODO -} + // TODO + + let highestValueArray = []; + let genreChildrenArray = []; + let genreNonFictionArray = []; + let genreCookingArray = []; + + // books.forEach(element => element.); + + books.forEach((element) => { + if (element.genre === "children") { + genreChildrenArray.push(element); + } else if (element.genre == "cooking") { + genreCookingArray.push(element); + } else { + genreNonFictionArray.push(element); + } + }); + + genreChildrenArray.sort((r1, r2) => + r1 < r2 ? 1 : (r1.rating > r2.rating) ? -1 : 0 + ); + genreCookingArray.sort((r1, r2) => + r1 < r2 ? 1 : r1.rating > r2.rating ? -1 : 0 + ); + + genreNonFictionArray.sort((r1, r2) => + r1 < r2 ? 1 : r1.rating > r2.rating ? -1 : 0 + ); + + highestValueArray.push(genreChildrenArray[0].title); + highestValueArray.push(genreCookingArray[0].title); + highestValueArray.push(genreNonFictionArray[0].title); + + console.log(highestValueArray); + return highestValueArray; +} /* ======= Book data - DO NOT MODIFY ===== */ const BOOKS = [ - { - title: "The Lion, the Witch and the Wardrobe", - genre: "children", - rating: 4.7 - }, - { - title: "Sapiens: A Brief History of Humankind", - genre: "non-fiction", - rating: 4.7 - }, - { - title: "Nadiya's Fast Flavours", - genre: "cooking", - rating: 4.7 - }, - { - title: "Harry Potter and the Philosopher's Stone", - genre: "children", - rating: 4.8 - }, - { - title: "A Life on Our Planet", - genre: "non-fiction", - rating: 4.8 - }, - { - title: "Dishoom: The first ever cookbook from the much-loved Indian restaurant", - genre: "cooking", - rating: 4.85 - }, - { - title: "Gangsta Granny Strikes Again!", - genre: "children", - rating: 4.9 - }, - { - title: "Diary of a Wimpy Kid", - genre: "children", - rating: 4.6 - }, - { - title: "BOSH!: Simple recipes. Unbelievable results. All plants.", - genre: "cooking", - rating: 4.6 - }, - { - title: "The Book Your Dog Wishes You Would Read", - genre: "non-fiction", - rating: 4.85 - }, -] - + { + title: "The Lion, the Witch and the Wardrobe", + genre: "children", + rating: 4.7, + }, + { + title: "Sapiens: A Brief History of Humankind", + genre: "non-fiction", + rating: 4.7, + }, + { + title: "Nadiya's Fast Flavours", + genre: "cooking", + rating: 4.7, + }, + { + title: "Harry Potter and the Philosopher's Stone", + genre: "children", + rating: 4.8, + }, + { + title: "A Life on Our Planet", + genre: "non-fiction", + rating: 4.8, + }, + { + title: + "Dishoom: The first ever cookbook from the much-loved Indian restaurant", + genre: "cooking", + rating: 4.85, + }, + { + title: "Gangsta Granny Strikes Again!", + genre: "children", + rating: 4.9, + }, + { + title: "Diary of a Wimpy Kid", + genre: "children", + rating: 4.6, + }, + { + title: "BOSH!: Simple recipes. Unbelievable results. All plants.", + genre: "cooking", + rating: 4.6, + }, + { + title: "The Book Your Dog Wishes You Would Read", + genre: "non-fiction", + rating: 4.85, + }, +]; /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the highest rated book in each genre", () => { - expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual(new Set( - [ - "The Book Your Dog Wishes You Would Read", - "Gangsta Granny Strikes Again!", - "Dishoom: The first ever cookbook from the much-loved Indian restaurant" - ] - )); -}); \ No newline at end of file + expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual( + new Set([ + "The Book Your Dog Wishes You Would Read", + "Gangsta Granny Strikes Again!", + "Dishoom: The first ever cookbook from the much-loved Indian restaurant", + ]) + ); +}); diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..b4a7340a 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,17 @@ function generateFibonacciSequence(n) { // TODO + let fibonacciNumber = 0; + let fibonacciArray = [0,1]; + for (let i = 1; i < n; i++) { + fibonacciNumber = (fibonacciArray[i] + fibonacciArray[i - 1]); + fibonacciArray.push(fibonacciNumber); + + + } + // console.log(fibonacciArray); + fibonacciArray.pop(); + return fibonacciArray; } /* ======= TESTS - DO NOT MODIFY ===== */