-
-
Notifications
You must be signed in to change notification settings - Fork 382
Glasgow | 26-ITP-May | Sandani Kannangara | Sprint 2 |Coursework #1366
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
SandzSoft
wants to merge
14
commits into
CodeYourFuture:main
Choose a base branch
from
SandzSoft: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
Show all changes
14 commits
Select commit
Hold shift + click to select a range
94cce41
Fix SyntaxError caused by redeclaring parameter in 0.js
SandzSoft dfee1f4
Fix parameter redeclaration and scope errors in 1.js
SandzSoft 5ada6fb
Fix invalid identifier error in 2.js
SandzSoft da11234
Fix missing return statement in 0.js
SandzSoft 8b63877
Fix return statement order in 1.js
SandzSoft 12aa8d4
Fix function to use parameter instead of global variable
SandzSoft fa6cd5a
Implement calculateBMI function in 1-bmi.js
SandzSoft 3325a8e
Implement upper snake case conversion in 2-cases.js
SandzSoft e92a6bb
Implement pence to pounds conversion in 3-to-pounds.js
SandzSoft 8764b8a
Add explanations for time formatting exercise in time-format.js
SandzSoft 3fcfa2e
Implement 12-hour clock formatting in format-time.js
SandzSoft 7d47a35
Fix review feedback on 12-hour clock formatting function
SandzSoft ee752c5
Fix review feedback on mandatory-debug 2.js
SandzSoft b504277
restore package.json
SandzSoft 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 | ||
| // Predict and explain : | ||
| //The code will throw a SyntaxError : 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) { | ||
| // Original code : | ||
| /*function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| }*/ | ||
|
|
||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // Corrected code : | ||
| function capitalise(str) { | ||
| let capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitaliseStr; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| // call the function capitalise with a string input : | ||
| capitalise("javascript"); | ||
|
|
||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // interpretation of the error message / Why an error is occurring : | ||
| //SyntaxError: Identifier 'str' has already been declared. | ||
| //str is already declared as a function parameter. | ||
| //Parameter is already a variable inside the function. | ||
| //Then, try to declare it again using let str as a local parameter. | ||
| //JavaScript does NOT allow two variables with the same name in the same function scope. |
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,28 @@ | ||
| // Predict and explain first... | ||
| // Predict and explain: | ||
| // Error occurs because decimalNumber is declared twice inside the function, and it is also a local variable not accessible outside the function. | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| // Original code | ||
| /*function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| console.log(decimalNumber);*/ | ||
|
|
||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // My explanation : | ||
| // decimalNumber is already declared as a function parameter, so it cannot be declared again with const inside the function. | ||
| // Also, decimalNumber only exists inside the function, so it cannot be used in console.log outside the function. | ||
|
|
||
| console.log(decimalNumber); | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // =============> write your explanation here | ||
| // Corrected code : | ||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| return percentage; | ||
| } | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| 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,33 @@ | ||
| // Predict and explain : | ||
| // The code will throw a SyntaxError because a number is used as a parameter name. | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
| // My prediction of the error : | ||
| //SyntaxError: Unexpected identifier | ||
|
|
||
| // =============> write your prediction of the error here | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // Original code : | ||
| /*function square(3) { | ||
| return num * num; | ||
| }*/ | ||
|
|
||
| // =============> write the error message here | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // =============> explain this error message here | ||
| // The error message : | ||
| //SyntaxError: Unexpected number | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // =============> write your new code here | ||
| // Explaination of the error message : | ||
| // Function parameters must be valid variable names (identifiers). | ||
| // The code uses the number 3 as a parameter, but numbers cannot be used as parameter names. | ||
|
|
||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // Corrected code : | ||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(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,14 +1,29 @@ | ||
| // Predict and explain first... | ||
| // My prediction : | ||
| // The function will print 320. | ||
| // Then the second console.log will print: | ||
| // "The result of multiplying 10 and 32 is undefined" | ||
| // because multiply() does not return a value. | ||
|
|
||
| // =============> write your prediction here | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| function multiply(a, b) { | ||
| // Original code : | ||
| /*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 | ||
| // my explanation : | ||
| // multiply() uses console.log() to display the result, but it does not return it. | ||
| // Functions that do not return a value return undefined by default. | ||
| // Therefore, multiply(10, 32) evaluates to undefined in the template string. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| //new code : | ||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,28 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // Predict and explain : | ||
| // The function will return undefined. | ||
| // Therefore, the output will be: | ||
| // "The sum of 10 and 32 is undefined" | ||
|
|
||
| function sum(a, b) { | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // Original code : | ||
| /*function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/ | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // My explanation : | ||
| // The return statement ends the function immediately. | ||
| // Because return is on its own line, the expression a + b is never executed. | ||
| // As a result, the function returns undefined. | ||
|
|
||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| //new code : | ||
| function sum(a, b) { | ||
| return a + b; | ||
| } | ||
| console.log(`The sum of 10 and 32 is ${sum(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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,51 @@ | ||
| // Predict and explain first... | ||
| // Predict and explain : | ||
| //The getLastDigit function always returns the last digit of 103, because it does not use the input value passed to it. Instead, it uses the global variable num. | ||
| //So every call returns 3. | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| const num = 103; | ||
| // My Prediction of the output of the Original code : | ||
| //The last digit of 42 is 3 | ||
| //The last digit of 105 is 3 | ||
| //The last digit of 806 is 3 | ||
|
|
||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| //Original code : | ||
| /*const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| return num.toString().slice(-1); | ||
| } | ||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`);*/ | ||
|
|
||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // output of the Original code : | ||
| //Answer : | ||
| //The last digit of 42 is 3 | ||
| //The last digit of 105 is 3 | ||
| //The last digit of 806 is 3 | ||
|
|
||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| // Explaination of why the output the way it is : | ||
| // The function does not use its parameter correctly. | ||
| // It ignores the value passed into it and instead uses the global variable num. | ||
| // That is why all outputs are the same. | ||
|
|
||
| //---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| //new code : | ||
| const num = 103; | ||
| function getLastDigit(num) { | ||
| return num.toString().slice(-1); | ||
| } | ||
|
|
||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
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.
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.
Note: Deleting the global
numis optional. Within the function block,numis resolved to the parameternum.If you are interested in the topic, you can looking up these two concepts, identifier scope and
identifier resolution, in the context of JavaScript programming. ChatGPT can give a good explanation.