-
-
Notifications
You must be signed in to change notification settings - Fork 396
London | 26-ITP-May | Eyob Zeray | Sprint 2 | Course Work #1546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eyob-tech
wants to merge
1
commit into
CodeYourFuture:main
Choose a base branch
from
eyob-tech:coursework/sprint-2
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // ==============> This will throw a SyntaxError, because 'str' is already declared as the function's parameter, and line 8 tries to declare a new variable with the same name using let. | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitalisedStr; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| // ==============> The error is "SyntaxError: Identifier 'str' has already been declared". This happens because you can't declare a new variable with let using a name that's already taken - in this case, the parameter str. | ||
| // ==============> function capitalise(str) { | ||
| // let capitalisedStr = ${str[0].toUpperCase()}${str.slice(1)}; | ||
| // return capitalisedStr; | ||
| // } |
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,20 +1,23 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // | ||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // ==============> This will throw a SyntaxError because decimalNumber is already declared as the function's parameter, and line 9 tries to redeclare it with const. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
| // Try playing computer with the example to work out what will happen | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
| console.log(convertToPercentage(0.5)); | ||
|
|
||
| // =============> write your explanation here | ||
| // ==============> The error is "SyntaxError: Identifier 'decimalNumber' has already been declared". It happens for the same reason as before - you can't redeclare a variable with the same name as an existing function parameter using const. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // ==============> function convertToPercentage(decimalNumber) { | ||
| // const percentage = ${decimalNumber * 100}%; | ||
| // return percentage; | ||
| // } | ||
| // console.log(convertToPercentage(0.5)); | ||
|
|
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,20 +1,18 @@ | ||
| // Predict and explain first BEFORE you run any code. | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
| // this function should square any number but instead | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
| // ==============> This will throw a SyntaxError, because 3 is used as the parameter name in function square(3), but parameter names can't be numbers - they need to be valid identifiers like "num". | ||
|
|
||
| // =============> write your prediction of the error here | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
| // ==============> The error is "SyntaxError: Unexpected number" | ||
|
|
||
| // =============> explain this error message here | ||
| // ==============> This happens because 3 is a number, not a valid parameter name. Parameter names must be identifiers, like "num" - JavaScript doesn't know what to do with a number in that position. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| // ==============> function square(num) { | ||
| // return num * num; | ||
| // } |
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,14 +1,16 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // ==============> This will print "320" first (from inside the function), then print "The result of multiplying 10 and 32 is undefined", because multiply() doesn't return a value - it only logs it. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // ==============> The function was using console.log to display the answer, instead of returning it. This meant when the outer console.log tried to use the value from multiply(10, 32), it got undefined instead of the actual number, since the function returned nothing. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // ==============> function multiply(a, b) { | ||
| // return a * b; | ||
| // } |
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,13 +1,15 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // ==============> This will print "The sum of 10 and 32 is undefined", because line 5 has a bare "return;" with nothing after it - this immediately exits the function and returns undefined. Line 6 (a + b) never runs. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // ==============> The function had "return;" on its own line, followed by "a + b;" on the next line. Once JavaScript hits return; with nothing after it, the function ends immediately and returns undefined - the a + b line is unreachable and never executes. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // ==============> function sum(a, b) { | ||
| // return a + b; | ||
| // } |
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.
Is there a better choice than multiplying and then dividing by the same number?
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.
Good point — yeah,
toFixed()is simpler for this. Something likeNumber(bmi.toFixed(1))would do the same job in one step instead of the multiply/round/divide trick. I'll keep it as is for this PR since it's already passing, but I'll usetoFixed()for rounding in future exercises. Thanks for the tip!