-
-
Notifications
You must be signed in to change notification settings - Fork 279
London 9 - Marziyeh Azhdari - JS-Core-1 - Week 3 #173
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // { | ||
| // // Use IntelliSense to learn about possible attributes. | ||
| // // Hover to view descriptions of existing attributes. | ||
| // // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| // "version": "0.2.0", | ||
| // "configurations": [ | ||
| // { | ||
| // "type": "node", | ||
| // "request": "launch", | ||
| // "name": "Launch Program", | ||
| // "skipFiles": [ | ||
| // "<node_internals>/**" | ||
| // ], | ||
| // "program": "${workspaceFolder}/1-exercises/B-while-loop/exercise.js" | ||
| // } | ||
| // ] | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,16 @@ | |
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| let result = 0; | ||
| let i = 0 * 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. This could be |
||
| do{ | ||
| result += i * 2; | ||
| i++; | ||
| }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. Great work here Mari! |
||
|
|
||
| return result | ||
| } | ||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
| console.log(evenNumbersSum(0)); // should output 0 | ||
| console.log(evenNumbersSum(10)); // should output 90 | ||
| console.log(evenNumbersSum(10)); // should output 90 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,21 @@ const AGES = [ | |
|
|
||
| // TODO - Write for loop code here | ||
|
|
||
| for(i = 0;i <= WRITERS.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. The comparison should be |
||
| const result=`${WRITERS[i]} is ${AGES[i]} years old ` | ||
| console.log(result) | ||
| } | ||
|
|
||
|
|
||
| //I did with forEach as well with same result | ||
|
|
||
| // WRITERS.forEach((num1, index) => { | ||
| // const num2 = AGES[index]; | ||
| // const result = `${num1} is ${num2} years old` | ||
| // console.log(result); | ||
| // }); | ||
|
|
||
|
|
||
| /* | ||
| The output should look something like this: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,27 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let arr =[]; | ||
| for (let article of allArticleTitles){ | ||
| if (article.length <= 65){ | ||
| arr.push(article) | ||
| } | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 arr = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| arr.push(allArticleTitles[i].split(" ").length); | ||
| } | ||
| return allArticleTitles[arr.indexOf(Math.min(...arr))]; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,15 +34,29 @@ function titleWithFewestWords(allArticleTitles) { | |
| (Hint: remember that you can also loop through the characters of a string if you need to) | ||
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| let arr=[] | ||
| for (let article of allArticleTitles){ | ||
| for (let char of article){ | ||
| if (char>="0" && char<="9"){ | ||
|
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! 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 could have also used regular expressions here: |
||
| arr.push (article); | ||
| break; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| return arr | ||
| } | ||
|
|
||
| /* | ||
| 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 sum=0; | ||
| for (let article of allArticleTitles){ | ||
| sum+=article.length; | ||
| } | ||
| return Math.round(sum/allArticleTitles.length) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,11 @@ | |
| */ | ||
|
|
||
| function factorial(input) { | ||
| // TODO | ||
| let sum = 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. Very small detail, but this is not a sum but a product. The function is correct though. |
||
| for (i = 1; i <= input; i++){ | ||
| sum *= i | ||
| } | ||
| return sum | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
This function works. Another way to write it which for me it's a bit more readable would be like this:
function evenNumbers(n) {let num= 0 ;let arr =[]while(num < n){arr.push(2*num)num ++}I find it more natural that the comparison is between n and num but both of them are correct.