diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..d19c3708 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -9,14 +9,17 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 -let a; +// Example 1 +//we are declaring a variable "a" without initializing it. When we try to log the value of "a" to the console, we get undefined because we haven't assigned a value to it yet. +let a= 3 console.log(a); // Example 2 +//we have a function called "sayHello" which initializes a local variable called "message" and doesn't return anything. When we call "sayHello" and assign the result to the "hello" variable, the value of "hello" becomes undefined because the function doesn't return anything. function sayHello() { let message = "Hello"; + return message } let hello = sayHello(); @@ -24,13 +27,16 @@ console.log(hello); // Example 3 +//we have a function called "sayHelloToUser" that expects a parameter called "user". When we call "sayHelloToUser" without passing any arguments, the "user" parameter is undefined, which causes the function to log "Hello undefined" to the console. function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser('baki'); // Example 4 +// we have an array called "arr" with three elements. When we try to access the fourth element using the index 3, we get undefined because the array doesn't have a fourth element. In JavaScript, when you try to access an index that doesn't exist in an array, the value returned is undefined. let arr = [1,2,3]; +arr.push(4) console.log(arr[3]); diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..a8bbd45f 100644 --- a/1-exercises/B-array-literals/exercise.js +++ b/1-exercises/B-array-literals/exercise.js @@ -5,8 +5,12 @@ */ 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); +} +let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares +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..b7d898cb 100644 --- a/1-exercises/C-array-get-set/exercise.js +++ b/1-exercises/C-array-get-set/exercise.js @@ -5,13 +5,17 @@ */ function first(arr) { - return; // complete this statement + + return arr[0]; } function last(arr) { - return; // complete this statement + + return arr[arr.length - 1]; + } + /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/1-exercises/C-array-get-set/exercises2.js b/1-exercises/C-array-get-set/exercises2.js index 6b6b007a..d2f3d963 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -7,7 +7,7 @@ */ let numbers = [1, 2, 3]; // Don't change this array literal declaration - +numbers.push(4) /* 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..e4f3a97b 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -28,8 +28,13 @@ const AGES = [ // TODO - Write for loop code here -/* -The output should look something like this: +for(let i ; i title.length <= 65); + } /* @@ -15,6 +18,13 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let fewestWordsTitle = allArticleTitles[0]; + for (let i = 1; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].split(" ").length < fewestWordsTitle.split(" ").length) { + fewestWordsTitle = allArticleTitles[i]; + } + } + return fewestWordsTitle; } /* @@ -24,6 +34,17 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + let headlinesWithNums = []; + //Return an array containing all the headlines which contain a number + for (let i = 0; i < allArticleTitles.length; i++) { + for (let j = 0; j < allArticleTitles[i].length; j++) { + if (!isNaN(parseInt(allArticleTitles[i][j]))) { + headlinesWithNums.push(allArticleTitles[i]); + break; + } + } + } + return headlinesWithNums; } /* @@ -32,6 +53,14 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + // return the average number of characters in an article title + + let totalChars = 0; + for (let i = 0; i < allArticleTitles.length; i++) { + totalChars += allArticleTitles[i].length; + } + let avgChars = Math.round(totalChars / allArticleTitles.length); + return avgChars; } diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..025a8ade 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -11,11 +11,11 @@ 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 + [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL + [340.69, 342.45, 334.69, 333.2, 327.29], // MSFT + [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN + [2951.88, 2958.13, 2938.33, 2928.3, 2869.45], // GOOGL + [1101.3, 1093.94, 1067.0, 1008.87, 938.53], // TSLA ]; /* @@ -34,9 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO -} + let output = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + const priceList = closingPricesForAllStocks[i]; + const totalPrice = priceList.reduce(function (a, b) { + return a + b; + }, 0); + const average = totalPrice / priceList.length; + const averageFixed = average.toFixed(2); + + output.push(parseFloat(averageFixed)); + } + return output; +} /* We also want to see what the change in price is from the first day to the last day for each stock. Implement the below function, which @@ -47,11 +59,22 @@ 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) */ -function getPriceChanges(closingPricesForAllStocks) { - // TODO +function getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) { + let changePrice = []; + for (let i = 0; i < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.length; i++) { + let priceList = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i]; + const diffirent = priceList.reduce(function (a, b) { + a = priceList[0]; + b = priceList[priceList.length - 1]; + return b - a; + }, 0); + changePrice.push(parseFloat(diffirent.toFixed(2))); + } + + return changePrice; } -/* +/* 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,32 +86,44 @@ function getPriceChanges(closingPricesForAllStocks) { The stock ticker should be capitalised. The price should be shown with exactly 2 decimal places. */ -function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO +function highestPriceDescriptions( + CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, + STOCKS +) { + const descriptions = []; + for (let i = 0; i < STOCKS.length; i++) { + const stockName = STOCKS[i]; + const prices = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i]; + const highestPrice = Math.max(...prices); + const formattedPrice = highestPrice.toFixed(2); + descriptions.push( + `The highest price of ${stockName.toUpperCase()} in the last 5 days was ${formattedPrice}` + ); + } + return descriptions; } - /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { - 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/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..f909e197 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,9 +15,20 @@ function generateFibonacciSequence(n) { // TODO + + const sequence = [0, 1]; + + // Generate the remaining numbers in the sequence + for (let i = 2; i < n; i++) { + sequence.push(sequence[i - 1] + sequence[i - 2]); + } + + // Return the generated sequence + return sequence; } -/* ======= TESTS - DO NOT MODIFY ===== */ + +/* ======= TESTS - DO NOT MODIFY ===== test("should return the first 10 numbers in the Fibonacci Sequence", () => { expect(generateFibonacciSequence(10)).toEqual( [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] @@ -34,4 +45,4 @@ test("should return the first 15 numbers in the Fibonacci Sequence", () => { expect(generateFibonacciSequence(15)).toEqual( [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] ); -}); \ No newline at end of file +});*/ \ No newline at end of file