-
-
Notifications
You must be signed in to change notification settings - Fork 389
Birmingham | 26-ITP-May | Ogbemi Mene | Sprint 2 | coursework #1439
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
meneogbemi42-bit
wants to merge
18
commits into
CodeYourFuture:main
Choose a base branch
from
meneogbemi42-bit:coursework/sprint2
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.
+176
−48
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
754dd3a
my predicted code
7851560
my correct code for percentage conversion
af9f6d7
correcting and predicting code. my correction
4c6b444
correcting error message for square num
3ba86a1
correction for multiplication function
66591aa
correction for square function
e01a422
correcting the sum function
8b5f715
new correction to the code
0520f97
code to return to decimal function
d548b1d
my code for uppercase
5208285
my reusable code for pounds
ba8e540
my explanation to the code
32e9c02
my code to fix the bug
443e1ec
new
261e7d9
re moval of the new
42c5fb6
solution for edge case
1712038
formated file
3d69494
Fix return statement placement in sum function
meneogbemi42-bit 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,30 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // If you try to run this code, it will throw a SyntaxError: Identifier 'str' has already been declared. | ||
|
|
||
| // 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 str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
|
|
||
| return str; | ||
| } | ||
| // When you define function capitalise(str), JavaScript automatically creates a local variable named str inside the function's scope, | ||
| // assigning it whatever value you pass into the function. | ||
| // The let keyword has a strict rule: you cannot declare a variable that already exists in the same scope. | ||
| // Because str was already claimed by the function's parameter, JavaScript immediately stops and throws a SyntaxError before it even tries to capitalize anything. | ||
| // this is the reason why the error is occurring. | ||
| // the if condition i added is to ensure that if an empty string is passed to the function, it will return the empty string instead of throwing an error. | ||
| // To fix the error, you can simply remove the let keyword and just assign the new value to str without redeclaring it. | ||
|
|
||
| function capitalise(str) { | ||
| if (typeof str !== "string") { | ||
| return str; | ||
| } | ||
|
|
||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
|
|
||
| } | ||
| console.log(capitalise("hello world!")); | ||
|
|
||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
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,14 +1,22 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // This code will actually run without throwing a red crash error, | ||
| // but it will print something very strange. the result of multiplying 10 and 32 is undefined. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // console.log() inside the function: Inside the multiply function, you used console.log(a * b). | ||
| // This instantly prints 320 to the screen, but it does not give that number back to the code that called it. | ||
| //The Missing return: In JavaScript, if a function does not explicitly use the word return, it automatically hands back undefined. | ||
| //The undefined string: Because multiply(10, 32) returns undefined, your outer console.log plugs undefined into the sentence, resulting in: "The result of multiplying 10 and 32 is undefined". | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // Finally, correct the code to fix the problem | ||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(multiply(10, 32)); |
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
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.
Really good explanation of scope and variable declarations here, just wanted to highlight the extra
ifstatement you added. Are you sure it handles the empty string case like you describe?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 is the best explanation i could come up with.
By checking typeof str !== "string", you are asking: "If the input is anything other than a string, stop processing and just return the input exactly as it is." If you send a string: It skips the if block and proceeds to do the work.
If you send a number: The if condition is true, so it returns the number and ignores the rest of the function.
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.
So your understanding of what the
ifstatement does (as you've explained here) is good, but that's not what you've said in the comments 🙂 You said:But as you have described, the if condition checks for the data type, not whether the string is empty. How would you check for that?
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.
yes, the initial statement was not correct, i looked at it critically a saw that this if statement will trow an error, because i did not add this ( | | ) operator, Because of the | | operator, if the string is empty, the function returns str. to handle it i will add || str.length === 0) to the if condition. and this will handle it.
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.
Please add this second condition then 🙂