-
-
Notifications
You must be signed in to change notification settings - Fork 279
Ldn-class-8-Saf Shah - JS Core 1 - Week 3 #22
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,17 @@ | |
| */ | ||
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| let arr = []; | ||
| let i = 0; | ||
| while (i%2 === 0 && n > 0 && n > arr.length) { | ||
| arr.push(i); | ||
| i+=2; | ||
| } | ||
| return arr.toString(); | ||
| } | ||
|
|
||
|
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 need to check if i is even here? |
||
| console.log(evenNumbers(3)); | ||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,16 @@ | |
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| let i = 0; | ||
| let sumTotal = 0; | ||
| do { | ||
| i++; | ||
| if (i % 2 == 0) { | ||
| sumTotal += i * 3; | ||
| } | ||
| } while (i < n); | ||
|
|
||
| return sumTotal; | ||
| } | ||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
|
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. If you print out each number as its being added here you're not actually adding the even numbers, you're adding the product of the even numbers of three. Have a double-check of the logic you're using here. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| Imagine we're making a weather app! | ||
|
|
||
| We have a list of cities that the user wants to track. | ||
| We also already have a temperatureService function which will take a city as a parameter and return a temparature. | ||
| We also already have a temperatureService function which will take a city as a parameter and return a temperature. | ||
|
|
||
| Implement the function below: | ||
| - take the array of cities as a parameter | ||
|
|
@@ -12,10 +12,14 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let newArray = []; | ||
| for (let i = 0; i < cities.length; i++){ | ||
| newArray.push("The temperature in " + cities[i] + " is " + temperatureService(cities[i]) + " degrees"); | ||
|
|
||
| } | ||
| return newArray; | ||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| function temperatureService(city) { | ||
|
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. Try and get into the habit of using a more descriptive name for your variable than 'newArray' |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,33 +5,75 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let newArray = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| if (allArticleTitles[i].length <= 65) { | ||
| newArray.push(allArticleTitles[i]); | ||
| } | ||
| } | ||
| return newArray; | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
| } | ||
| 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. Another way of doing this is to initialise newString with the first entry, and then you can just check each subsequent entry with only one condition |
||
|
|
||
| /* | ||
| 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 | ||
| let newClicks = []; | ||
|
|
||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| for (let letter of allArticleTitles[i]) { | ||
| 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. maybe you already know there is another easy way to find out number inside array I used that |
||
|
|
||
| } | ||
| } | ||
| 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)) | ||
| } | ||
|
|
||
|
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. you did it an easier way I changed my code after seeing yours thank you Saf! |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,8 +35,20 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let newAverage =[] | ||
| let sum = 0; | ||
| for(let i = 0; i < STOCKS.length; i++){ | ||
| 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)) * 1); | ||
| sum = 0; | ||
| } | ||
| return newAverage; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| We also want to see what the change in price is from the first day to the last day for each stock. | ||
| Implement the below function, which | ||
|
|
@@ -48,7 +60,12 @@ 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 | ||
| getPriceChange = []; | ||
| for (price of closingPricesForAllStocks) { | ||
| let priceChange = Number((price[price.length -1] - price[0]).toFixed(2)); | ||
| getPriceChange.push(priceChange); | ||
| } | ||
| return getPriceChange; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -60,11 +77,15 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| - Returns an array of strings describing what the highest price was for each stock. | ||
| For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33" | ||
| The test will check for this exact string. | ||
| The stock ticker should be capitalised. | ||
| The stock ticker should be capitalized. | ||
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| highestPriceLast5Days = []; | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| highestPriceLast5Days.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${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. I have seen it on google using Math.max() it really helps to make easier our work, anyway I also used another way to solve it. |
||
| return highestPriceLast5Days; | ||
| } | ||
|
|
||
|
|
||
|
|
||
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.
hey, Saf in line 11 when you give the condition while loop you can make it simply (n > arr.length) and it will work!