-
-
Notifications
You must be signed in to change notification settings - Fork 279
ZA_2-Nishka_Kisten-JavaScript-Core-1-Coursework-Week3 #65
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 |
|---|---|---|
|
|
@@ -7,7 +7,13 @@ | |
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| let i = 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. This is really well done.
Author
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. Will do! |
||
| let array =[]; | ||
| do{ | ||
| array.push(i * 2 ); | ||
| i++; | ||
| } while(i < n); | ||
| return array.reduce((a,b) => a + b, 0); | ||
| } | ||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| A for-of loop is a easy and way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements). | ||
| A for-of loop is an easy way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements). | ||
| */ | ||
|
|
||
| // TODO Use a for-of loop to output each of the tube stations below. | ||
|
|
@@ -11,6 +11,14 @@ let tubeStations = [ | |
| "Tottenham Court Road" | ||
| ]; | ||
|
|
||
| for (let value 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. Code works perfectly fine. Try and name your variables with meaningful names ; names like |
||
| console.log(value); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||
| let str = "codeyourfuture"; | ||
| let str = "codeyourfuture"; | ||
| for(let val of str) { | ||
| console.log(val.toUpperCase()); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,12 @@ function generateRandomNumber() { | |
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| let i ; | ||
|
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. Really well done! |
||
| do{ | ||
| i = generateRandomNumber(); | ||
|
|
||
| }while(i <= 50) | ||
| return i ; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,33 +5,60 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| var result = allArticleTitles.filter((n) => n.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. Good use of filter and the conditional to check for results before returning it.
Author
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. No.. It should probably return a message saying there are no articles that fit right? |
||
| if (result) { | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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) | ||
| */ | ||
| (you can assume words will always be separated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| } | ||
| var fewestWords = allArticleTitles[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. Excellent algorithm with the for loop and initializing the
Author
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. Will try it out though. Thanks! |
||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| var element = allArticleTitles[i]; | ||
| if (fewestWords.length > element.length) { | ||
| fewestWords = element; | ||
| } | ||
| } | ||
| return fewestWords; | ||
| }; | ||
|
|
||
|
|
||
| /* | ||
| The editor of the FT has realised that headlines which have numbers in them get more clicks! | ||
| The editor of the FT has realized 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 | ||
| // if(typeof allArticleTitles === "number") { | ||
| // return typeof allArticleTitles === "number"; | ||
| // } | ||
| titleWithNum = []; | ||
| var hasNumber = /\d/; | ||
| for(let element of allArticleTitles) { | ||
| if (hasNumber.test(element)){ | ||
| titleWithNum.push(element);} | ||
| } return titleWithNum; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 total = 0; | ||
| let sum; | ||
| for(let element of allArticleTitles){ | ||
| total= total + element.length; | ||
| sum = Math.round(total/allArticleTitles.length); | ||
| }return sum; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let total= []; | ||
| let tot= 0; | ||
| let sum = 0; | ||
| for(let element of closingPricesForAllStocks){ | ||
| sum = element.reduce((a, b) => a + b); | ||
| tot = sum / 5; | ||
|
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 use of .reduce() function.
Author
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 wrong.. Should I use .length? I will try and do it without magic numbers. |
||
| total.push(Math.round(tot * 100) / 100); | ||
| } return total | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +55,18 @@ 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 total = []; | ||
| let priceChange = 0; | ||
| for (let prices of closingPricesForAllStocks) { | ||
| let last = prices.slice(-1); | ||
| // console.log(last); | ||
| // console.log(prices[0]); | ||
| priceChange = last - prices[0]; | ||
| // total.push(priceChange.toFixed(2)); | ||
| total.push( Math.round(priceChange * 100) / 100); | ||
|
|
||
| // console.log(priceChange); | ||
| } return total; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +82,23 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let newStocks = []; | ||
| let total = []; | ||
| let message = ""; | ||
| let last; | ||
| for (let stock of closingPricesForAllStocks) { | ||
| stock.sort((a, b) => b - a); | ||
| // last = stock[0]; | ||
| // total.push( Math.round(last * 100) / 100); | ||
| total.push(stock); | ||
| } | ||
| for (let i = 0; i < stocks.length; i++) { | ||
| let roundedUp =(total[i][0]).toFixed(2); | ||
| // let roundedUp = Math.round((total[i][0]) * 100) / 100; | ||
| message = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + roundedUp; | ||
| newStocks.push(message); | ||
| } | ||
| return newStocks; | ||
| } | ||
|
|
||
|
|
||
|
|
||
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.
Nicely done!