diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..2ddb8021 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -9,12 +9,12 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1: because no value assign to the variable "a" let a; console.log(a); -// Example 2 +// Example 2: because function does not have any return. function sayHello() { let message = "Hello"; } @@ -23,7 +23,7 @@ let hello = sayHello(); console.log(hello); -// Example 3 +// Example 3: because there is no argument when call the function. function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -31,6 +31,6 @@ function sayHelloToUser(user) { sayHelloToUser(); -// Example 4 +// Example 4: because arr[3] dose not have value. let arr = [1,2,3]; console.log(arr[3]); diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..7cb80908 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,16 @@ function evenNumbers(n) { // TODO + let i=0; + let evenNumber = 0; + let arr = []; + while(i="0" && char<="9"){ + arr.push (article); + break; + } + + } + } + return arr + } /* @@ -32,6 +61,12 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let sum=0; + for (let article of allArticleTitles){ + sum+=article.length; + } + return Math.round(sum/allArticleTitles.length) + } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..038de7e4 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -33,8 +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) { - // TODO + function getAveragePrices(closingPricesForAllStocks) { + // TODOfunction + + let averageArr=[]; + for (let closingPricesForStock of closingPricesForAllStocks){ + let sum =0; + for (let item of closingPricesForStock){ + sum=sum+item; + } + averageArr.push(Number((sum/closingPricesForStock.length).toFixed(2))); + + } + return averageArr; + } /* @@ -48,10 +60,17 @@ 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 + // TODO + let arr=[]; + for (let closingPricesForStock of closingPricesForAllStocks){ + arr.push(Number((closingPricesForStock[closingPricesForStock.length-1]-closingPricesForStock[0]).toFixed(2))); + + } + return arr; + } -/* + /* 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 - Takes 2 parameters: @@ -63,8 +82,15 @@ function getPriceChanges(closingPricesForAllStocks) { The stock ticker should be capitalised. The price should be shown with exactly 2 decimal places. */ + function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let arr=[]; + for (let i=0;i a.rating - b.rating); + let childSorted = child.sort((a, b) => a.rating - b.rating); + + let cookSorted = cook.sort((a, b) => a.rating - b.rating); + resultArr.push(scFictionSorted[scFictionSorted.length - 1].title); + resultArr.push(childSorted[childSorted.length - 1].title); + resultArr.push(cookSorted[cookSorted.length - 1].title); + + return resultArr; +} /* ======= 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..427297e9 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,13 @@ */ function generateFibonacciSequence(n) { - // TODO + // TODo + let arr=[0,1]; + for (let i=0;i