-
-
Notifications
You must be signed in to change notification settings - Fork 546
Manchester | 26-ITP-Sep |Precious Moses | Sprint 3 | Course-work-3 #1585
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
1d8957c
cb5fd1c
3dcb7b7
14998ae
4b9c01a
386a826
ed354a3
02c297f
d3c778f
0411379
c6ad8eb
d2880fb
2bddb02
3f06814
1b3d99b
56326fb
cae8927
ad792cd
31d8426
0f097a3
1d56388
220a714
762e97f
39b87c3
3a1178c
9558f43
667b02a
0dcdd7f
f76aa55
5433734
24548db
c094e79
1552743
9f517e7
36c8ab9
ff1d8b8
8f96ad7
31d1742
e2a617b
8e24d75
f2a0d61
ca723f9
409baf9
c53c5ed
c50dd38
b4452ff
329939a
33f9efb
b89e5b7
2d81e8b
7fc8853
fee9c33
1768716
d51b30d
d2547c9
7d17723
2fd1da4
91327e6
feee6ba
acaa7de
efb0135
8e26e40
7ea0781
e3228c5
93a453c
4c05247
a5a7c63
0fe7ef9
ac9bd5d
2f14c92
b148d65
fb5b6b5
b6de5f3
e642a44
36e4464
9953c8b
ad65ef3
4f756c0
c5f7033
bfbaf1b
6fc17ad
38ee172
c72aac2
ebe4d77
be45ad6
42c8e32
a818573
117c886
ce0df58
ca134f6
b5f6fa6
8613d97
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,29 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // Prediction | ||
| // A SyntaxError is thrown with message: 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 | ||
| // Error message interpreted ====> The error message simply means the name 'str' was declared twice in the same scope | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
|
|
||
| // function capitalise(str) { | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| // } | ||
| // capitalise("moses"); | ||
| // =============> write your explanation here | ||
| // The error here is because the parameter 'str' and the variable 'let str' share the same name in he same scope, thus the SyntaxError. | ||
| // To fix this error, I could either give the variable a different name or reassign the parameter 'str' without 'let', since no new declaration is made | ||
|
|
||
| // =============> write your new code here | ||
| // function capitalise(str) { | ||
| // let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return capitalised; | ||
| // } | ||
| // console.log(capitalise("moses")); | ||
| // | | ||
| function capitalise(str) { | ||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
| console.log(capitalise("moses")); |
|
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 job spotting the scope conflict and fixing the code. Quick question: since decimalNumber * 100 is already a short expression, could you return it directly inside the template string without declaring a separate percentage variable?
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. Thanks for the feedback. I just enjoy adding more details to my code
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. I fixed it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,32 @@ | ||
| // Predict and explain first... | ||
| // My prediction is the program would throw a SyntaxError, because 'decimalNumber' has already been declared as a parameter and also has been declared as a new variable in the same scope | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| //An error will occur because 'const decimalNumber = 0.5' clashes with the parameter of the same name. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| // function convertToPercentage(decimalNumber) { | ||
| // // The parameter 'decimalNumber' is created as a variable inside the function's scope. | ||
| // const decimalNumber = 0.5; | ||
| // // tries to declare new variable called 'decimalNumber' in the same scope, where one already exists as a parameter | ||
| // // cannot declare same name twice in one scope with 'const' | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
| // return percentage; | ||
| // } | ||
| // // As a result of the conflict nothing runs. | ||
| // console.log(decimalNumber); | ||
| // // This line is outside the function and would not parse, as 'decimalNumber' only exists inside 'convertToPercentage' as a parameter. | ||
|
|
||
| // =============> write your explanation here | ||
| // Parameters and variables declared inside a function cannot be accessed outside it. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber){ | ||
| return `${decimalNumber * 100}%`; | ||
|
|
||
| } | ||
| console.log(convertToPercentage(0.5)); |
|
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. Hello! Great job identifying why passing a number literal in a function definition causes a SyntaxError and fixing it with a proper parameter name. Quick question: when naming parameters like
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. Fixed |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,28 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // The parameter is written as a value instead of a variable name | ||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // The program will throw a SyntaxError | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // function square(3) { | ||
| // return num * num; | ||
| // } | ||
|
|
||
| // =============> write the error message here | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // In the 'function square(3)' JavaScript parser expected a valid parameter name it could use as a variable | ||
| // Since '3' is a number literal, not a valid identifier, the parser cannot understand it and throws a syntax error. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
| function square(number){ | ||
| return number * number; | ||
| } | ||
| console.log(square(77)); | ||
|
|
||
|
|
|
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. Hello! Great job tracing the execution order and explaining why functions without a return statement default to returning Quick question: since the function now uses
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. Thanks for the feedback! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,26 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // My prediction is '320' would be printed first followed by "The result of multiplying 10 and 32 is undefined" | ||
| // This occurs because 'multiply()' does not return anything | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
| // function multiply(a, b) { | ||
| // console.log(a * b); | ||
| // } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| // console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // The template literal calls 'multiply(10, 32) to build the string | ||
| // Inside multiply, 'console.log(a * b)' runs immediately, printing '320' | ||
| // This happens before the outer 'console.log' line finishes, because JavaScript has to evaluate 'multiply(10, 32)' first to know what to put in the template | ||
| // 'multiply's' function body only contains a 'console.log' statement without a 'return' keyword | ||
| // A function with no explicit return statement returns 'undefined' by default. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function multiply(a, b){ | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); |
|
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. Hello! Great job spotting how automatic semicolon insertion affects the Quick question: since the expression
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. Thanks for the feedback. I enjoy detailing my code. I'll fix it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,23 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // The code seems like it would log 'The sum of 10 and 32 is 42' since function 'sum(10, 32)' seems to add two numbers | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| // function sum(a, b) { | ||
| // return; | ||
| // a + b; | ||
| // } | ||
|
|
||
| // console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // When the code was run it logged 'The sum of 10 and 32 is undefined' | ||
| // The bug is in line 7 | ||
| // 'return' is automatically assigned a semi-colon if it is followed by a line break, making line 8 unreachable | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function sum(a, b){ | ||
| return a + b; | ||
| } | ||
| console.log (`The sum of 10 and 32 is ${sum(10, 32)}`); |
|
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. Hello! Great job identifying how the global constant variable shadowed the arguments and correctly refactoring the function to accept a parameter. Quick question: why is passing values directly as parameters considered safer and more flexible than relying on outer variables like
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. Thanks for the feedback! |
|
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. Hello! Great job writing the Quick question: since
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. Thanks for the feedback! |
|
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. Hello! Great job writing a robust function that handles extra spaces and validates the input type. Quick question: since this is a fundamentals exercise, do you think using a regular expression like |
|
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. Hello! Great job transforming the snippet into a reusable function and effectively handling different input lengths using string methods like Quick question: since your logic relies on removing the trailing 'p', how might your function behave if an input accidentally omits the 'p' or includes extra whitespace? |
|
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. Hello! Great job tracing the execution step by step and using temporary tracking variables to verify your answers. Quick question: while using global variables helped you trace the code here, why can relying on global variables for tracking state be risky in larger applications? |
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.
Hello! Great job fixing the scope error.
Quick question: since the template string already produces the exact result you need, could you return it directly without reassigning the parameter, and how might that improve code simplicity?
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.
Thanks for feedback! I think I might have got carried away. I fixed it.
It would improve code simplicity as it cuts out an unnecessary step.