-
-
Notifications
You must be signed in to change notification settings - Fork 279
Rahwa Ghebremichael London-8 JS-1 Week-3 #21
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 |
|---|---|---|
|
|
@@ -6,9 +6,19 @@ | |
| */ | ||
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| // TODO | ||
| let arr = []; | ||
| let i = 0; | ||
| while (i % 2 === 0 && n > 0 && n > arr.length) { | ||
| arr.push(i); | ||
| i += 2; | ||
|
|
||
| } | ||
|
|
||
| return arr.toString(); // changes the numbers to strings | ||
|
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 above prints every value on a different line. As an exercise let do this to print them as a comma separated string:
|
||
| } | ||
|
|
||
| evenNumbers(3); // should output 0,2,4 | ||
| evenNumbers(0); // should output nothing | ||
| evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18 | ||
|
|
||
| console.log(evenNumbers(3)); // should output 0,2,4 | ||
| console.log(evenNumbers(0)); // should output nothing | ||
| console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,10 @@ const AGES = [ | |
| ]; | ||
|
|
||
| // TODO - Write for loop code here | ||
| for(i=0; i < WRITERS.length; i++) { | ||
| console.log(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.
|
||
|
|
||
|
|
||
| /* | ||
| 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(tubes of tubeStations) { | ||
| console.log(tubes); | ||
| } | ||
|
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.
|
||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||
| let str = "codeyourfuture"; | ||
| let result = str.toUpperCase(); | ||
| for(letter of result) { | ||
| console.log(letter); | ||
| } | ||
|
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.
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,17 @@ | |
| function generateRandomNumber() { | ||
| console.log("Generating number..."); | ||
| return Math.round(Math.random() * 100); | ||
|
|
||
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| let number; | ||
| do{number = generateRandomNumber() | ||
|
|
||
| } | ||
| while(number <= 50) | ||
| return number; | ||
|
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: put a newline after |
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,33 +5,73 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| // TODO | ||
| let newArrayTitle = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| if (allArticleTitles[i].length <= 65) { | ||
| newArrayTitle.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return newArrayTitle; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| 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 newString = '' | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| if (newString.length < 1 || newString.split(" ").length > allArticleTitles[i].split(" ").length) { | ||
| newString = allArticleTitles[i]; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return newString; | ||
| } | ||
|
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. Cool solution ⭐ |
||
| /* | ||
| 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 | ||
| let newClicks = []; | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| for (let letter of allArticleTitles[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. Hi Rahwa, as we are discovering there are many different ways of doing the same thing in JavaScript. |
||
| if (letter === "0"|| | ||
| letter === "1"|| | ||
| letter === "2"|| | ||
| letter === "3"|| | ||
| letter === "4"|| | ||
| letter === "5"|| | ||
| letter === "6"|| | ||
| letter === "7"|| | ||
| letter === "8"|| | ||
| letter === "9") { | ||
| newClicks.push(allArticleTitles[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. I like that you've solved the problem correctly with the ways you knew 🥇 There is a function |
||
|
|
||
| } | ||
| } | ||
| return newClicks; | ||
| } | ||
|
|
||
| /* | ||
| 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 totalLength = 0; | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| totalLength += allArticleTitles[i].length | ||
| } | ||
| return (Math.round(totalLength / allArticleTitles.length)) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let newAverage =[] | ||
| let sum = 0; | ||
| for (let i = 0; i < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.length; 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. In the function, instead of using |
||
| for (let j = 0; j < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length; j++) { | ||
| sum += CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i][j]; | ||
| } | ||
| newAverage.push( | ||
| (sum / CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length).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. As an alternative multiply the number by 100, then Math.round() it; then divide by 100 That way, you don't have to convert a number to a string , then multiply by 1 to convert it to a number again |
||
| 1 | ||
| ); | ||
|
|
||
| sum = 0; | ||
| } | ||
| return newAverage; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,9 +63,13 @@ 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 | ||
| // TODO | ||
| let newPriceChange = []; | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| newPriceChange.push((closingPricesForAllStocks[i][4] - closingPricesForAllStocks[i][0]).toFixed(2) * 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. See my above comment regarding Math.round() |
||
| } | ||
| return newPriceChange; | ||
| } | ||
|
|
||
| /* | ||
| As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. | ||
| Implement the below function, which | ||
|
|
@@ -65,6 +84,15 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| highestPrice =[]; | ||
| for(let i=0; i < closingPricesForAllStocks.length; i++) { | ||
| let highestNum = closingPricesForAllStocks[i].sort(function(a,b){ | ||
|
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!! |
||
| return b-a;} )[0]; | ||
| highestPrice.push( | ||
| "The highest price of " + stocks[i].toUpperCase() + " " + "in the last 5 days was " + highestNum.toFixed(2)); | ||
|
|
||
| } | ||
| return highestPrice; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,12 @@ | |
|
|
||
| function factorial(input) { | ||
| // TODO | ||
|
|
||
| for (let i = input - 1; i >= 1; i--) { | ||
| input= input*i; | ||
| console.log(input) | ||
| } | ||
| return input; | ||
|
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 one! ⭐ |
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,14 @@ | |
|
|
||
| function getHighestRatedInEachGenre(books) { | ||
| // TODO | ||
| let bookTitles=[]; | ||
| for(let i=0; i<books.length; i++){ | ||
| if(books[i].rating > 4.8){ | ||
| bookTitles.push(books[i].title) | ||
| } | ||
|
|
||
| } | ||
| return bookTitles; | ||
|
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's great that you've solved the problem by adding the highest rating manually. I am so happy you started looking into the extra exercises too. For finding the highest rating with the code (so that you can use the function with other types of books array and rating that |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,18 @@ | |
|
|
||
| function generateFibonacciSequence(n) { | ||
| // TODO | ||
|
|
||
| let fib = [0, 1]; | ||
| for(i = 2; i < n; i++) { | ||
| let y = fib[i-2] + fib[i-1]; | ||
| fib.push(y); | ||
| } | ||
|
|
||
|
|
||
| return fib; | ||
|
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. 💯 |
||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| test("should return the first 10 numbers in the Fibonacci Sequence", () => { | ||
| expect(generateFibonacciSequence(10)).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.
Hello Rahwa
Regarding this function I believe you ought to have another look.
Your output should be one string with commas e.g. 0,2,4
See if you can redo this function with a 'string' in mind. You want to build a string from "" and add even numbers to this string until 'n' is reached.