-
-
Notifications
You must be signed in to change notification settings - Fork 279
London Class 9- Leila Farsani- JavaScript-Core-3-Coursework-Week3 #163
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,16 @@ | |
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| } | ||
| let i = 0; | ||
| let even = []; | ||
| while (i < 2 * n) { | ||
| even.push(i); | ||
| i += 2; | ||
| } | ||
| return even.toString(); | ||
| } | ||
| console.log(evenNumbers()); | ||
|
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 logs out the array fine, looks good. |
||
|
|
||
|
|
||
| evenNumbers(3); // should output 0,2,4 | ||
| evenNumbers(0); // should output nothing | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,19 +5,26 @@ | |
| */ | ||
|
|
||
| const BIRTHDAYS = [ | ||
| "January 7th", | ||
| "February 12th", | ||
| "April 3rd", | ||
| "April 5th", | ||
| "May 3rd", | ||
| "July 11th", | ||
| "July 17th", | ||
| "September 28th", | ||
| "November 15th" | ||
| "January 7th", | ||
| "February 12th", | ||
| "April 3rd", | ||
| "April 5th", | ||
| "May 3rd", | ||
| "July 11th", | ||
| "July 17th", | ||
| "September 28th", | ||
| "November 15th", | ||
| ]; | ||
|
|
||
| function findFirstJulyBDay(birthdays) { | ||
| // TODO | ||
| // TODO | ||
| let i = 0; | ||
| while (i < birthdays.length) { | ||
| if (birthdays[i][1] === "u") { | ||
|
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 works, but what if later on more birthdays are added to the array including ones in June? Is it a bit more robust and readable to look for the whole word |
||
| return birthdays[i]; | ||
| } | ||
| i++; | ||
| } | ||
| } | ||
|
|
||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,13 @@ | |
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| // TODO | ||
| let i = 0; | ||
| let sum = n * (n - 1); | ||
| do { | ||
| return sum; | ||
|
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 returning instantly from the evenNumbersSum function, so it only ever executes once. Line 12 ( |
||
| i++; | ||
| } while (i < 5); | ||
| } | ||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,10 @@ const AGES = [ | |
| ]; | ||
|
|
||
| // TODO - Write for loop code here | ||
|
|
||
| for (i = 0; i < WRITERS.length; i++) { | ||
| let text = WRITERS[i] + " is " + AGES[i] + " years 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. Great! Also have a look at string templates, see if you prefer that syntax. |
||
| console.log(text); | ||
| } | ||
| /* | ||
| The output should look something like this: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,13 @@ let tubeStations = [ | |
| "Tottenham Court Road" | ||
| ]; | ||
|
|
||
| for (let tube of tubeStations){ | ||
| console.log(tube); | ||
| } | ||
|
|
||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||
| let str = "codeyourfuture"; | ||
|
|
||
| for (let x of str){ | ||
| console.log(x.toLocaleUpperCase()); | ||
|
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 didn't know about |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,14 @@ function generateRandomNumber() { | |
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| // TODO - implement using a do-while loop | ||
| let n; | ||
| do { | ||
| n = generateRandomNumber(); | ||
| } while (n <= 50); | ||
|
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. 👍 |
||
| return n; | ||
| } | ||
| console.log(getRandomNumberGreaterThan50()); | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,29 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| // TODO | ||
| let articleArr = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| if (allArticleTitles[i].length <= 65) { | ||
| articleArr.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return articleArr; | ||
| } | ||
|
|
||
| /* | ||
| 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 wordsCount= []; | ||
| for (let i = 0; i < allArticleTitles.length; i++){ | ||
| wordArticle = allArticleTitles[i].split(""); | ||
|
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 words need to be split by a space ie |
||
| wordsCount.push(wordArticle.length); | ||
| } | ||
| let index = wordsCount.indexOf(Math.min(...wordsCount)) | ||
|
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. Wow nice use of the spread operator |
||
| return allArticleTitles[index]; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -24,6 +37,14 @@ function titleWithFewestWords(allArticleTitles) { | |
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| let numArticle = []; | ||
| let pattern =/[0-9]/g; | ||
|
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. Do you know what the |
||
| for (let i=0; i<allArticleTitles.length; i++){ | ||
| if (allArticleTitles[i].search(pattern)!= -1 ){ | ||
|
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. Better to prefer |
||
| numArticle.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return numArticle; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -32,6 +53,13 @@ function headlinesWithNumbers(allArticleTitles) { | |
| */ | ||
| function averageNumberOfCharacters(allArticleTitles) { | ||
| // TODO | ||
| let total = 0; | ||
| for (i=0; i < allArticleTitles.length; i++){ | ||
| total += allArticleTitles[i].length; | ||
| average = total / allArticleTitles.length; | ||
| roundedAverage = 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. Perfect - will round to the closest integer, either upwards or downwards 👍 |
||
| } | ||
| return roundedAverage; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let averageArr = []; | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| let total = 0; | ||
| for (let j=0; j < closingPricesForAllStocks[i].length; j++){ | ||
| total += closingPricesForAllStocks[i][j]; | ||
| } | ||
| let average = total / closingPricesForAllStocks[i].length; | ||
| let roundedAverage = Number(average.toFixed(2)); | ||
|
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 technique |
||
| averageArr.push(roundedAverage); | ||
|
|
||
| } | ||
| return averageArr; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -49,6 +61,12 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| */ | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| let changeArr=[]; | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++){ | ||
| let change = closingPricesForAllStocks[i][closingPricesForAllStocks.length - 1] - closingPricesForAllStocks[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 works 👍 |
||
| changeArr.push(Number(change.toFixed(2))); | ||
| } | ||
| return changeArr; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +82,13 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| // TODO | ||
| let maxArray = []; | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++){ | ||
| let maxStocks = Math.max(...closingPricesForAllStocks[i]).toFixed(2); | ||
|
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.
|
||
| maxArray.push("The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + maxStocks); | ||
| } | ||
| return maxArray; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,32 @@ | |
|
|
||
| function getHighestRatedInEachGenre(books) { | ||
| // TODO | ||
| } | ||
| let childrenGenre = []; | ||
| let nonFictionGenre = []; | ||
| let cookingGenre = []; | ||
| let booksArr = []; | ||
| for (let i = 0; i < books.length; i++) { | ||
| if (books[i].genre === "children") { | ||
|
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. Works perfectly |
||
| childrenGenre.push(books[i]); | ||
| } else if (books[i].genre === "non-fiction") { | ||
| nonFictionGenre.push(books[i]); | ||
| } else { | ||
| cookingGenre.push(books[i]); | ||
| } | ||
| } | ||
| let childrenSorted = childrenGenre.sort((a, b) => a.rating - b.rating); | ||
|
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! Also consider |
||
| let nonFictionSorted = nonFictionGenre.sort((a, b) => a.rating - b.rating); | ||
| let cookingSorted = cookingGenre.sort((a, b) => a.rating - b.rating); | ||
|
|
||
|
|
||
| booksArr.push(childrenSorted[childrenSorted.length - 1].title); | ||
| booksArr.push(cookingSorted[cookingSorted.length - 1].title); | ||
| booksArr.push(nonFictionSorted[nonFictionSorted.length - 1].title); | ||
|
|
||
| return booksArr; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= Book data - DO NOT MODIFY ===== */ | ||
|
|
||
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!