-
-
Notifications
You must be signed in to change notification settings - Fork 279
WM4 - Kerim Zamir - JS Core 1 - Week 3 #78
base: main
Are you sure you want to change the base?
Changes from all commits
f969bba
6ca81bf
35f5fc2
9394d44
196b454
7dba218
09a5173
252cb70
4e4dd64
0f13180
0c4bc99
7ff16db
b504ad9
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,8 +5,24 @@ | |
| The list of numbers should start with 0. n is being passed in as a parameter. | ||
| */ | ||
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| function evenNumbers(n) | ||
| { | ||
| let total = "0"; | ||
| let calc = 0; | ||
|
|
||
| if (n === 0) | ||
| { | ||
| return console.log(""); | ||
| } | ||
|
|
||
| for(let i = 1; i < n; i++) | ||
| { | ||
| calc = calc + 2; | ||
|
|
||
| total = total + ", " + calc; | ||
| } | ||
|
|
||
| return console.log(total); | ||
|
Comment on lines
+8
to
+25
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. The instructions said that you should use a while loop. |
||
| } | ||
|
|
||
| evenNumbers(3); // should output 0,2,4 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,7 +27,10 @@ const AGES = [ | |||||
| ]; | ||||||
|
|
||||||
| // TODO - Write for loop code here | ||||||
|
|
||||||
| for (let i = 0; i < WRITERS.length; i++) | ||||||
| { | ||||||
| console.log(WRITERS[i] + " is " + AGES[i] + " old") | ||||||
|
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.
Suggested change
|
||||||
| } | ||||||
| /* | ||||||
| The output should look something like this: | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,15 @@ let tubeStations = [ | |||||
| "Tottenham Court Road" | ||||||
| ]; | ||||||
|
|
||||||
| for (i of tubeStations) | ||||||
|
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.
Suggested change
I think it will be better to use a meaningful variables names, it's easy to read and understand ("i" generally refers to index which is not the case here) |
||||||
| { | ||||||
| console.log(i); | ||||||
| } | ||||||
|
|
||||||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||||||
| let str = "codeyourfuture"; | ||||||
|
|
||||||
| for (i of str) | ||||||
| { | ||||||
| console.log(i.toUpperCase()) | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,15 @@ | |
| - Hint: you can call the temperatureService function from your function | ||
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| function getTemperatureReport(cities) | ||
|
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. Good work! |
||
| { | ||
| let TemperatureArray = []; | ||
|
|
||
| for (let city of cities) | ||
| { | ||
| TemperatureArray.push("The temperature in " + city + " is " + temperatureService(city) + " degrees"); | ||
| } | ||
| return TemperatureArray; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -60,4 +67,4 @@ test("should return a temperature report for the user's cities (alternate input) | |
|
|
||
| test("should return an empty array if the user hasn't selected any cities", () => { | ||
| expect(getTemperatureReport([])).toEqual([]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,16 @@ function generateRandomNumber() { | |
| return Math.round(Math.random() * 100); | ||
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| function getRandomNumberGreaterThan50() | ||
| { | ||
| let total; | ||
| do | ||
| { | ||
| (total = generateRandomNumber()); | ||
| } | ||
| while (total <= 50); | ||
|
|
||
| return total; | ||
|
Comment on lines
+12
to
+21
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 think the use of 'total' as the name of the variable creates a little confusion as we don't really calculate any total in this function |
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,72 @@ | ||
| /* | ||
| Imagine you are working on the Financial Times web site! They have a list of article titles stored in an array. | ||
|
|
||
| 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 | ||
|
|
||
| function potentialHeadlines(allArticleTitles) | ||
| { | ||
| let AcceptedArticles = [] | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) | ||
| { | ||
| if (allArticleTitles[i].length <= 65) | ||
| { | ||
| AcceptedArticles.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return AcceptedArticles; | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
|
|
||
| function titleWithFewestWords(allArticleTitles) | ||
| { | ||
| const shorter = (left, right) => left.length <= right.length ? left : right; | ||
|
|
||
| return allArticleTitles.reduce(shorter); | ||
| } | ||
|
|
||
|
Comment on lines
+27
to
33
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. Could you please add some comments to this function, I didn't really understand it? |
||
| /* | ||
| 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 | ||
|
|
||
| function headlinesWithNumbers(allArticleTitles) | ||
| { | ||
| let AcceptedArticles = []; | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) | ||
| { | ||
| if (/\d/.test(allArticleTitles[i])) | ||
| { | ||
| AcceptedArticles.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return AcceptedArticles; | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
| } | ||
|
|
||
| function averageNumberOfCharacters(allArticleTitles) | ||
| { | ||
| let AllChar = 0; | ||
| let Average = 0; | ||
| for (let i = 0; i < allArticleTitles.length; i++) | ||
| { | ||
| AllChar += (allArticleTitles[i].length); | ||
| } | ||
|
|
||
| return Math.round(Average = AllChar / allArticleTitles.length); | ||
| } | ||
|
|
||
| /* ======= List of Articles - DO NOT MODIFY ===== */ | ||
| const ARTICLE_TITLES = [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,8 +33,14 @@ 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 | ||||||||||||||||||||||
| //https://jrsinclair.com/articles/2019/five-ways-to-average-with-js-reduce/ | ||||||||||||||||||||||
| function getAveragePrices(closingPricesForAllStocks) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| //const reduce = r => i => a => a.reduce(r, i); | ||||||||||||||||||||||
| //conatiner = getting items => got items.reduce => add / length .map previous => round to 2 dec | ||||||||||||||||||||||
| const average = (closingPricesForAllStocks.map((array) => array.reduce((r, current) => r + current / array.length, 0))).map((element) => parseFloat(element.toFixed(2))); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return average; | ||||||||||||||||||||||
|
Comment on lines
+36
to
+43
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. Could you please delete unnecessary comments to make the code cleaner :D |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /* | ||||||||||||||||||||||
|
|
@@ -47,8 +53,12 @@ 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(closingPricesForAllStocks) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| const PriceChange = (closingPricesForAllStocks.map((array) => array[array.length - 1] - array[0])).map((element) => parseFloat(element.toFixed(2))); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return PriceChange; | ||||||||||||||||||||||
|
Comment on lines
+57
to
+61
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.
Suggested change
It's better to use to variables to make code more readable |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /* | ||||||||||||||||||||||
|
|
@@ -63,11 +73,26 @@ 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(closingPricesForAllStocks, stocks) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| let HighestPriceText = []; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for(let i = 0; i < closingPricesForAllStocks.length; i++) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| let AllHighestPrices = 0; | ||||||||||||||||||||||
| for(let i2 = 0; i2 < closingPricesForAllStocks[i].length; i2++) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| if(closingPricesForAllStocks[i][i2] > AllHighestPrices) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| AllHighestPrices = closingPricesForAllStocks[i][i2]; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| HighestPriceText.push("The highest price of " + STOCKS[i].toUpperCase() + " in the last 5 days was " + AllHighestPrices.toFixed(2)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return HighestPriceText; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||||||||||||||||||||||
| test("should return the average price for each stock", () => { | ||||||||||||||||||||||
| expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
Hi Kerim, I've looked through your code and it's great! One suggestion I have here however is using a while loop instead of a for loop.
Great work!