-
-
Notifications
You must be signed in to change notification settings - Fork 279
LONDON-10/BAKI TUNCER/JAVASCRIPT-3 #247
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,9 @@ | |
| */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks perfect! |
||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| // to create a new array containing only article titles that have 65 characters or less. | ||
| return allArticleTitles.filter(title => title.length <= 65); | ||
|
|
||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well done Baki |
||
| /* | ||
|
|
@@ -15,6 +18,13 @@ function potentialHeadlines(allArticleTitles) { | |
| */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice way to solve this one! |
||
| 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) { | |
| */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this works! |
||
| 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) { | |
| */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good to me 👍 |
||
| 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; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Baki check here this is my review for this week |
||
| /* | ||
| 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", | ||
| ]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,20 @@ | |
|
|
||
| function generateFibonacciSequence(n) { | ||
| // TODO | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job on this one! |
||
| 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] | ||
| ); | ||
| }); | ||
| });*/ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice solution 👍