-
-
Notifications
You must be signed in to change notification settings - Fork 390
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
base: main
Are you sure you want to change the base?
Changes from all commits
754dd3a
7851560
af9f6d7
4c6b444
3ba86a1
66591aa
e01a422
8b5f715
0520f97
d548b1d
5208285
ba8e540
32e9c02
443e1ec
261e7d9
42c5fb6
1712038
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 |
|---|---|---|
| @@ -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 | ||
| 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)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // This code will run without throwing a crash error, but it will print an incorrect result. | ||
| // The output will be:This code will run without throwing a crash error, but it will print an incorrect result. | ||
| // The output will be: The sum of 10 and 32 is undefined | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
|
|
@@ -8,6 +10,13 @@ function sum(a, b) { | |
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> the explanation: | ||
| // The function sum has a return statement before the addition operation, which causes the function to return undefined immediately. | ||
| // To fix this, the return statement should be placed after the addition operation. | ||
| // because the function is not a string i have to remove the backticks and use a normal string concatenation to print the result correctly. | ||
|
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.
What happens if you tried to use the backticks here with a function that returns a number?
Author
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 use backticks with a function that returns a number, JavaScript will automatically convert that number into a string so it can be combined with the rest of your text. just wanted to indicate that i was not working with string functions |
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function sum(a, b) { | ||
| return a + b; | ||
| } | ||
| console.log(sum(10, 32)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,27 @@ | |
| // You will need to declare a function called toPounds with an appropriately named parameter. | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
| function toPounds(penceString) { | ||
| const penceStringWithoutTrailingP = penceString.substring( | ||
| 0, | ||
| penceString.length - 1 | ||
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
|
|
||
| return `£${pounds}.${pence}`; | ||
| } | ||
|
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. Be wary of code formatting - because you copied and pasted this code from another file, none of the code within the function body is indented, which makes it harder to read. Prettier is a really useful extension to help with formatting. You don't have to use it, but either way - please fix the formatting here!
Author
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. done and corrected. thanks for the correction. |
||
|
|
||
| console.log(toPounds("399p")); | ||
| console.log(toPounds("1250p")); | ||
| console.log(toPounds("5p")); | ||
| console.log(toPounds("70p")); | ||
| console.log(toPounds("0p")); | ||
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.