-
-
Notifications
You must be signed in to change notification settings - Fork 279
ZA2-Harerimana-Dieudonne/JavaScript-Core-1Coursework-Week3 #74
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,6 +7,18 @@ | |
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| i = 0; | ||
| y = n; | ||
| let x = ""; | ||
| if (n > 0) { | ||
| while (i < y) { | ||
| x += i + ","; | ||
| i += 2; | ||
| } | ||
| console.log(x); | ||
| } else { | ||
| // console.log("nothing"); | ||
| } | ||
|
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 try, but it doesn't cover all the use cases. |
||
| } | ||
|
|
||
| evenNumbers(3); // should output 0,2,4 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,14 @@ const BIRTHDAYS = [ | |
| ]; | ||
|
|
||
| function findFirstJulyBDay(birthdays) { | ||
| // TODO | ||
| // TODO | ||
| let capture = ""; | ||
| for (let i = 0; i < birthdays.length; i += 6) { | ||
| const index = birthdays[i]; | ||
| index; | ||
| capture += index; | ||
| console.log(index); | ||
| } | ||
| } | ||
|
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. Not quite right. Please have a second look. Hint: Try to use a return value in the function |
||
|
|
||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
| // console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,14 @@ | |
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| let i = 0; | ||
| let message = "Total"; | ||
| let capture = ""; | ||
| do { | ||
| let sum = message + capture[i]; | ||
| console.log(sum); | ||
| i * 2; | ||
| } while (i < n); | ||
|
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. Not quite right. Please review the stopping condition and also the type of the variable |
||
| } | ||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,12 @@ | |
|
|
||
|
|
||
| // Change the below code to use a for loop instead of a while loop. | ||
| let i = 0; | ||
| while(i < 26) { | ||
| console.log(String.fromCharCode(97 + i)); | ||
| i++; | ||
| // let i = 0; | ||
| // while(i < 26) { | ||
| // console.log(String.fromCharCode(97 + i)); | ||
| // i++; | ||
| // } | ||
| for (let i = 0; i < 29; i++) { | ||
| console.log(String.fromCharCode(94 + 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. Great job |
||
| } | ||
| // The output shouldn't change. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,18 @@ const AGES = [ | |
| ]; | ||
|
|
||
| // TODO - Write for loop code here | ||
| let capture = ""; | ||
| for (let i = 0; i < WRITERS.length; i++) { | ||
| const name = WRITERS[i] | ||
| const age = AGES[i] | ||
| console.log(name, age, "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 formatting could have been better, but great job on this task |
||
| } | ||
| // for (var i in WRITERS) { | ||
| // for (var a in AGES) { | ||
| // capture += WRITERS[i] + AGES[a]; | ||
| // console.log(capture); | ||
| // } | ||
| // } | ||
|
|
||
| /* | ||
| The output should look something like this: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,5 +12,19 @@ let tubeStations = [ | |
| ]; | ||
|
|
||
|
|
||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||
| // TODO Use a for-of loop to capitalize and output each letter in the string seperately. | ||
| let str = "codeyourfuture"; | ||
|
|
||
| // TODO 1 | ||
| for (let i = 0; i < tubeStations.length; i++) { | ||
| const index = tubeStations[i]; | ||
| console.log(index); | ||
| } | ||
|
|
||
| // TODO 2 | ||
| let capture = "" | ||
| for (let i = 0; i < str.length; i++) { | ||
| capture += str[i]; | ||
| } | ||
|
|
||
| console.log(capture.toUpperCase()); | ||
|
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. Not quite right. There is a difference between a |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,13 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| // TODO | ||
| let temperature = []; | ||
| for (city of cities) { | ||
| let temp = temperatureService(city); | ||
| temperature.push("The temperature in", city, " is ", temp, " degrees"); | ||
|
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 logic is good, the only issue is the way formatting. The idea of pushing the content of this line is excellent, but it needs to be done as a single entry. The way it is currently formatted, js thinks you are pushing 05 differents entries. |
||
| } | ||
| return temperature; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,11 @@ function generateRandomNumber() { | |
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| i = 0; | ||
| do { | ||
| i += generateRandomNumber(); | ||
| } while (i < 50); | ||
| return 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. Great job here |
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,32 +6,49 @@ | |
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| const article = allArticleTitles.filter((article) => article.length <= 65); | ||
| return article; | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
| // TODO | ||
| const outputOfArticle = allArticleTitles[0]; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| if ( | ||
| allArticleTitles[i].split(" ").length < outputOfArticle.split(" ").length | ||
| ) | ||
| outputOfArticle = 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.
|
||
| } | ||
| return outputOfArticle; | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
| } | ||
| const title = allArticleTitles.filter((article) => article.match(/(\d+)/)); | ||
| return title | ||
|
|
||
| /* | ||
| 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 | ||
| const total = 0; | ||
| for (let title of allArticleTitles) { | ||
| total += title.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.
|
||
| } | ||
| let average = total / allArticleTitles.length; | ||
| return 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. if you update the |
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,9 +34,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| // TODO | ||
| return closingPricesForAllStocks.map((priceArr) => { | ||
| return Number( | ||
| (priceArr.reduce((tot, num) => tot + num) / priceArr.length).toFixed(2) | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
|
|
@@ -49,6 +54,9 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| */ | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| return closingPricesForAllStocks.map((priceArr) => { | ||
| return Number((priceArr[priceArr.length - 1] - priceArr[0]).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. Great job on these two functions. |
||
|
|
||
| /* | ||
|
|
||
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.
Brilliant.