generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 193
Glasgow | ITP May '25 | Mirabelle Morah | Module-Structuring-and-Testing-Data | Sprint 2 #606
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
mirabellemorah
wants to merge
4
commits into
CodeYourFuture:main
Choose a base branch
from
mirabellemorah: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
4 commits
Select commit
Hold shift + click to select a range
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,24 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
|
||
/* 💡Answer: To be honest, I get confused when I see template literals and understand the older version of writing JS better, using operators, but I am dedicated to trying my best in this. I see a lot of the use of 'str' in this and I suspect this is almost correct but may fail because of the variable name 'str' used one too many times, as a parameter and a variable. I know that slice deletes parts of a string and 'toUpperCase' could be to capitalise things. */ | ||
|
||
// call the function capitalise with a string input | ||
// interpret the error message and figure out why an error is occurring | ||
|
||
function capitalise(str) { | ||
/* function capitalise(str) { | ||
let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return str; | ||
} | ||
*/ | ||
|
||
// =============> What I get is a "SyntaxError: Identifier 'str' has already been declared". This means that in line 10 of my code, I have declared the variable 'str' twice, once as a parameter and once as a variable. This is not allowed in JavaScript, as each variable must have a unique name within its scope. To fix this, I need to remove the 'let' keyword from the second declaration of 'str', so that I am simply reassigning the value to the existing parameter instead of trying to declare it again. | ||
|
||
// =============> write your explanation here | ||
// =============> write your new code here | ||
|
||
function capitalise(str) { | ||
str = str[0].toUpperCase() + str.slice(1); | ||
return str; | ||
} | ||
|
||
console.log(capitalise("wheew! Done it without template literals :)")); // Should print "Wheew! Done it without template literals :)" |
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,20 +1,23 @@ | ||
|
||
// Predict and explain first BEFORE you run any code... | ||
|
||
// this function should square any number but instead we're going to get an error | ||
|
||
// =============> write your prediction of the error here | ||
// =============> 💡Answer: Mhmm there seems to be an error in line 8 because you can't use a number/integer alone as the start name of a parameter. It should be more of a name or string or add the number at the end of the value/name | ||
|
||
function square(3) { | ||
/* function square(3) { | ||
return num * num; | ||
} | ||
*/ | ||
|
||
// =============> write the error message here | ||
// =============> write the error message here: "SyntaxError: Unexpected number" and "ReferenceError: num is not defined" | ||
|
||
// =============> explain this error message here | ||
// =============> explain this error message here: Using a number alone is not a valid identifier name in JavaScript. Identifiers must start with a letter, underscore (_), or dollar sign ($). Also , the variable `num` is not defined anywhere in the function, which leads to a ReferenceError when trying to use it. | ||
|
||
// Finally, correct the code to fix the problem | ||
|
||
// =============> write your new code here | ||
|
||
// =============> | ||
function square(Now3) { | ||
return Now3 * Now3; | ||
} | ||
|
||
console.log(square(3)); // Output: 9 |
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 | ||
// =============> Answer 💡: does not have a return statement, so it will return undefined | ||
|
||
function multiply(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 | ||
// =============> write your explanation here: The code shows the message | ||
// "320 The result of multiplying 10 and 32 is undefined" | ||
// It is because the multiply function does not return a value, it only logs the result to the console. | ||
|
||
// 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)}`); // logs "The result of multiplying 10 and 32 is 320" |
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,20 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
// =============> write your prediction here: Answer 💡: looks like it will fail because of the way return is written | ||
|
||
function sum(a, b) { | ||
/*function sum(a, b) { | ||
return; | ||
a + b; | ||
} | ||
|
||
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
*/ | ||
|
||
// =============> write your explanation here | ||
// =============> write your explanation here: The error occurs because the return statement is used incorrectly. It has a semicolon after it, which causes the function to return undefined immediately, and the line `a + b;` is never executed. | ||
// 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)}`); // logs "The sum of 10 and 32 is 42" |
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. Again if we go back to the above comment, this function is missing a parameter, thus passing an argument to it will simply be ignored. In other programming languages passing an argument to a function which does not have parameters will throw an exception. |
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,40 @@ | ||
// Predict and explain first... | ||
|
||
// Predict the output of the following code: | ||
// =============> Write your prediction here | ||
// =============> Write your prediction here -- Answer 💡: no parameter for function getLastDigit() | ||
|
||
const num = 103; | ||
/*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)}`); | ||
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 | ||
// | ||
/* -- because there's no parameter, the console logs teh following for all the function calls | ||
The last digit of 42 is 3 | ||
The last digit of 105 is 3 | ||
The last digit of 806 is 3*/ | ||
|
||
// Explain why the output is the way it is | ||
// =============> write your explanation here | ||
// =============> This is as a result of the function always using the constant variable num, outside the function, which is set to 103, to get its answer. | ||
// 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 | ||
|
||
function getLastDigit(number) { | ||
return number.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)}`); | ||
|
||
// The function now takes a parameter 'number' and uses it to get the last digit of the number passed to it. This is because .slice(-1) is used to get the last character of the string representation of the number. |
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
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.
Another way to see this is also that in the wrong syntax example, we are passing the argument to the function when declaring the function. Instead we pass arguments to functions when we call the function, and pass parameters to function when we declare a function. This is a small but important distinction specifically when discussing code with other developers!
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.
Oh I didn't know, thank you for explaining to me. I'll keep learning