-
-
Notifications
You must be signed in to change notification settings - Fork 279
London10 - Khalil Alhaydr - JavaScript-Core-1-Coursework-Week3 #241
base: main
Are you sure you want to change the base?
Changes from all commits
ee71e91
f958bfc
3d38600
d9eef54
bca3876
9089b49
152adda
7a2bb77
7f276ed
8772e42
9dfd1f1
661b563
68c76c5
ac7883e
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 |
|---|---|---|
|
|
@@ -5,11 +5,11 @@ | |
| */ | ||
|
|
||
| function first(arr) { | ||
| return; // complete this statement | ||
| return arr[0]; // complete this statement | ||
|
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. Thought: This will return undefined if the array is empty. |
||
| } | ||
|
|
||
| function last(arr) { | ||
| return; // complete this statement | ||
| return arr[arr.length - 1]; // complete this statement | ||
|
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. Thought: This will return an error if the |
||
| } | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| */ | ||
|
|
||
| let numbers = [1, 2, 3]; // Don't change this array literal declaration | ||
| numbers[3] = 4; | ||
|
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. Suggestion: You could also do |
||
| numbers[0] = 1; | ||
|
|
||
| /* | ||
| DO NOT EDIT BELOW THIS LINE | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,18 @@ | |
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let resultArray = [] | ||
| for (let city of cities) { | ||
| let text = "The temperature in "+ city + " is " + temperatureService(city) + " degrees"; | ||
| resultArray.push(text) | ||
|
|
||
| } | ||
| return resultArray | ||
| } | ||
|
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. Comment: I saw you and Elena have a similar solution. I had another solution |
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| function temperatureService(city) { | ||
|
|
@@ -31,6 +40,9 @@ function temperatureService(city) { | |
|
|
||
| return temparatureMap.get(city); | ||
| } | ||
| // test("Should return an array of the same argument", () => { | ||
| // expect(getTemperatureReport(usersCities).length).toEqual(3) | ||
| // }); | ||
|
|
||
| test("should return a temperature report for the user's cities", () => { | ||
| let usersCities = [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,14 @@ | |
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let articleTitlesLessThan65 = [] | ||
| for (article of allArticleTitles) { | ||
| if (article.length <= 65) { | ||
| articleTitlesLessThan65.push(article) | ||
| } | ||
| } | ||
| return articleTitlesLessThan65 | ||
|
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. Suggestion: This could also be achieved by using a |
||
|
|
||
| } | ||
|
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. Comment: Same as mine |
||
|
|
||
| /* | ||
|
|
@@ -14,7 +22,27 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
|
|
||
| let shortestTitle; | ||
| // let shortestTitleWordCount = Infinity; | ||
|
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. Nitpick: Don't leave commented code in your solution |
||
| let shortestTitleWordCount; | ||
|
|
||
|
|
||
| let firstWord; | ||
| for (let title of allArticleTitles) { | ||
|
|
||
| let wordCount = title.split(" ").length; | ||
| if (wordCount < (shortestTitleWordCount) || (shortestTitle === undefined)){ | ||
| shortestTitleWordCount = wordCount | ||
| shortestTitle = title | ||
| } | ||
|
|
||
|
|
||
| } | ||
| return shortestTitle | ||
|
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. Comment: I see you use .split which I did not. Well done 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. Suggestion: This could also be achieved with a |
||
|
|
||
|
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. Nitpick: Don't leave too many blank lines in a function |
||
|
|
||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,15 +51,30 @@ function titleWithFewestWords(allArticleTitles) { | |
| (Hint: remember that you can also loop through the characters of a string if you need to) | ||
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| let arrayTitlesWithNumbers =[] | ||
| for (let title of allArticleTitles) { | ||
| if (/\d/.test(title)) { | ||
| arrayTitlesWithNumbers.push(title) | ||
| } | ||
| } | ||
| return arrayTitlesWithNumbers | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 sum = 0; | ||
| let count = 0; | ||
| for (let title of allArticleTitles) { | ||
| let wordCount = title.length; | ||
| sum += wordCount; | ||
| count += 1; | ||
| } | ||
| let average = sum / count; | ||
|
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. couldnt you use allArticleTitles.length - 1 instead count variable ? |
||
| return Math.round(average); | ||
|
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. Comment: Nice |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let sum = 0; | ||
| let averageArray = [] | ||
| for (let i = 0; i < 5; i++) { | ||
| for (let j = 0; j < 5; j++){ | ||
| sum += closingPricesForAllStocks[i][j] | ||
| } | ||
|
|
||
| let average = sum / 5; | ||
| averageArray.push(Math.round(average *100)/100) | ||
| sum = 0; | ||
| } | ||
| return averageArray | ||
|
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. Comment: Nice code! |
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +59,19 @@ 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 priceChangeArray = [] | ||
| let priceChange = 0 | ||
| let priceOnFirstDay = 0 | ||
| let priceOnLastDay = 0 | ||
| for (let i = 0; i < 5; i++) { | ||
| for (let j = 0; j < 5; j++) { | ||
| priceOnFirstDay = closingPricesForAllStocks[i][0] | ||
| priceOnLastDay = closingPricesForAllStocks[i][4] | ||
| } | ||
| priceChange = priceOnLastDay - priceOnFirstDay | ||
| priceChangeArray.push(Math.round(priceChange * 100) / 100) | ||
| } | ||
| return priceChangeArray | ||
| } | ||
|
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. Comment: You could also use array in array method. |
||
|
|
||
| /* | ||
|
|
@@ -63,11 +86,41 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The stock ticker should be capitalised. | ||
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
|
|
||
| function highestPriceFunction (closingPricesForAllStocks) { | ||
| let highestPriceArray = [] | ||
| let price = 0 | ||
| let highestPrice = 0 | ||
| for (let i = 0; i < 5; i++) { | ||
| for (let j = 0; j < 5; j++) { | ||
| if (closingPricesForAllStocks[i][j] > price) { | ||
| highestPrice = closingPricesForAllStocks[i][j] | ||
| price = highestPrice | ||
| } | ||
| } | ||
| highestPriceArray.push(highestPrice) | ||
| price = 0 | ||
| } | ||
| return highestPriceArray | ||
|
|
||
| } | ||
|
|
||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let resultArray = [] | ||
| highestPriceArray = highestPriceFunction (closingPricesForAllStocks) | ||
| for (let i = 0; i < 5; i++) { | ||
| for(let j = 0; j < 5; j++) { | ||
| if (i === j) { | ||
| resultArray.push("The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestPriceArray[j].toFixed(2)) | ||
| } | ||
| } | ||
| } | ||
| return resultArray | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| test("should return the average price for each stock", () => { | ||
| expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( | ||
|
|
||
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.
Thought: The function will output the string "Hello undefined" instead of just undefined