This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 279
London 9 - Lovelace - Howard Sun - JavaScript-Core1 - Coursework- Week3 #204
Open
howard-ss
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
howard-ss:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,55 @@ | ||
| /* | ||
| Sometimes when using loops, we'll want to execute the body of the loop at least once. We can make sure this happens by using a do-while loop. | ||
| - If the condition in a while loop is initially false, the body of the loop will never execute | ||
| - But in a do-while loop, because the condition is checked after the body, we know that it will always execute at least once | ||
| Sometimes when using loops, we'll want to execute the body of the loop | ||
| at least once. We can make sure this happens by using a do-while loop. | ||
| - If the condition in a while loop is initially false, the body of | ||
| the loop will never execute | ||
| - But in a do-while loop, because the condition is checked after | ||
| the body, we know that it will always execute at least once | ||
|
|
||
| Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) | ||
| Using a do-while loop, write a function which returns the sum of the | ||
| first n even numbers (starting from 0) | ||
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| } | ||
| // TODO | ||
|
|
||
| //1.own solution: initialize first even number is 0, sum is 0, counter from 0; | ||
| // let currEvenNumber = 0; | ||
| // let sum = 0; | ||
| // let i = 0; | ||
| // do { | ||
| // //sum of first n even number(0): | ||
| // sum += currEvenNumber; | ||
| // //next even number: | ||
| // currEvenNumber += 2; | ||
| // //next counter: | ||
| // i++; | ||
| // } while (i < n); //condition | ||
| // return sum; //return final output when condition is false. | ||
|
|
||
| //2.solution from google classroom review: | ||
| let sum = 0; | ||
| let i=0; | ||
| do { | ||
| sum += (i * 2); | ||
| i++; | ||
| }while(i < n); | ||
| return sum; | ||
|
|
||
| } | ||
| // for loop: solution | ||
| // let currEvenNumber = 0, | ||
| // sum = 0; | ||
| // // sum of first n even numbers | ||
| // for (let i = 0; i < n; i++) { | ||
| // sum += currEvenNumber; | ||
| // // next even number | ||
| // currEvenNumber += 2; | ||
| // } | ||
| // // required sum | ||
| // return sum; | ||
| // } | ||
|
|
||
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Keep up the good work