-
-
Notifications
You must be signed in to change notification settings - Fork 279
WM4_Dawit-Abraha_JavaScript-Core-1_Week-3 #92
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 |
|---|---|---|
|
|
@@ -10,7 +10,12 @@ let tubeStations = [ | |
| "Oxford Street", | ||
| "Tottenham Court Road" | ||
| ]; | ||
|
|
||
| for (let arr of tubeStations) { | ||
| console.log(arr); | ||
| } | ||
|
|
||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||
| let str = "codeyourfuture"; | ||
| for (arr of str){ | ||
|
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. it would be nice to declare variable - arr |
||
| console.log(arr.toUpperCase()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,34 +4,76 @@ | |
| The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less. | ||
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
|
|
||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| 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. Nice use of |
||
|
|
||
| // let newArray = []; | ||
| // for (let i = 0; i < allArticleTitles.length; i++){ | ||
| // if (allArticleTitles[i].length <= 65) { | ||
| // newArray.push(allArticleTitles[i]) | ||
| // } | ||
| // } | ||
| // return newArray; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| The editor of the FT likes short headlines with only a few words! | ||
| Implement the function below, which returns the title with the fewest words. | ||
| (you can assume words will always be seperated by a space) | ||
| */ | ||
|
|
||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| let array = allArticleTitles[0].split(" ").length; | ||
|
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. What do you think will happen on this line of code if there are no articles? Naming is hard. What data does the variable |
||
| let spaceCount; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| wordCount = allArticleTitles[i].split(" ").length; | ||
|
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. I suggest to add .trim() method before split ,Because if there is any space at first or in the end of string it will wrongly add to the number of words 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. What is missing from this line? |
||
| if (wordCount < array) { | ||
| array = wordCount; | ||
| spaceCount = i; | ||
| } | ||
| } | ||
| return allArticleTitles[spaceCount]; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| The editor of the FT has realised that headlines which have numbers in them get more clicks! | ||
| Implement the function below to return a new array containing all the headlines which contain a number. | ||
| (Hint: remember that you can also loop through the characters of a string if you need to) | ||
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| return allArticleTitles.filter(headline => /[0-9]/.test(headline)); | ||
|
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. I like this solution 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. Why do you like it? What is appealing about this code? 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. My comment is a sample of how beginners comment who make a review for beginners)) |
||
|
|
||
| // let result = []; | ||
| // for (let i = 0; i < allHeadLineTitles.length; i++) { | ||
| // let innersentece = allArticleTitles[i] | ||
| // for (let j = 0; j < innersentece.length; j++){ | ||
| // let value = innersentece[j] | ||
| // if (!isNaN(value) && value != " ") | ||
| // { | ||
| // result.push(innersentece) | ||
| // break; | ||
| // } | ||
| // } | ||
| // } | ||
| // return result; | ||
| } | ||
|
|
||
| /* | ||
| The Financial Times wants to understand what the average number of characters in an article title is. | ||
| Implement the function below to return this number - rounded to the nearest integer. | ||
| */ | ||
|
|
||
| function averageNumberOfCharacters(allArticleTitles) { | ||
| // TODO | ||
| let charCountSum = 0; | ||
| for (let i = 0; i < allArticleTitles.length; i++){ | ||
| charCountSum += allArticleTitles[i].length; | ||
| } | ||
| let average = charCountSum / allArticleTitles.length; | ||
| return Math.round(average); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| 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,7 +34,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let arr = []; | ||
| let average = 0; | ||
|
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. What is the benefit of defining this variable outside the for loop? |
||
| for (const stockPrices of closingPricesForAllStocks) { | ||
| let stockSum = 0; | ||
| for (const price of stockPrices) { | ||
| stockSum += price; | ||
| } | ||
| average = parseFloat((stockSum / stockPrices.length).toFixed(2)); | ||
| arr.push(average); | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +58,14 @@ 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 changeArray = []; | ||
| for (const stockPrices of closingPricesForAllStocks) { | ||
| let priceChange = parseFloat( | ||
| (stockPrices[stockPrices.length - 1] - stockPrices[0]).toFixed(2) | ||
| ); | ||
| changeArray.push(priceChange); | ||
| } | ||
| return changeArray; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,31 +81,52 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| const sortedPrices = closingPricesForAllStocks.map((prices) => | ||
| prices.sort((a, b) => b - a) | ||
| ); | ||
| return sortedPrices.map((price, index) => { | ||
|
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 use of |
||
| return `The highest price of ${stocks[ | ||
| index | ||
| ].toUpperCase()} in the last 5 days was ${price[0].toFixed(2)}`; | ||
| }); | ||
| // let result = [] | ||
| // for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| // let companyStock = closingPricesForAllStocks[i]; | ||
| // let highestValue = companyStock[0]; | ||
| // for (let j = 0; j < companyStock.length; j++) { | ||
| // if (companyStock[j] > highestValue) { | ||
| // highestValue = companyStock[j] | ||
| // } | ||
| // } | ||
| // result[i] = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestValue.toFixed(2) | ||
| // } | ||
| // return result; | ||
| } | ||
|
|
||
| // console.log( | ||
| // highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS) | ||
| // ); | ||
|
|
||
| /* ======= 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", | ||
| ]); | ||
| }); | ||
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.
I like that you used reduce method here