-
-
Notifications
You must be signed in to change notification settings - Fork 389
London | 26-ITP-May | Russom Gebremeskel | Sprint 2 | Coursework/sprint 2 #1458
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
a5e418c
598d02c
6de1b67
10512d8
3772da2
4bbefef
3ecc9ea
dffd727
7e1a0bf
313ea73
45bd5ef
2e7a6a3
c7ec47c
2345af3
01f907a
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,6 +1,8 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| // This will throw an error because there is a semicolon after return statement. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
|
|
@@ -9,5 +11,17 @@ function sum(a, b) { | |
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| //The sum of 10 and 32 is undefined | ||
|
|
||
| // The above error message is displayed when tested with node. | ||
| // The cause of the error message is the semicolon that is placed after the return statement. | ||
|
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. Did this actually cause an error message to be displayed?
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. It returns '' The sum of 10 and 32 is undefined". Initially I had assumed that this was happening because of the semicolon after return. Now I run the code again this time without the semicolon still get "undefined" message. I did some research online and it is actually not just because of the semicolon but because both the return statement and the expression "a+b" are not on the same line. |
||
|
|
||
| // 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)}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
|
|
||
| // num is declared outside of the function. So when getLastDigit is called it will return undefined. | ||
|
|
||
| const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
|
|
@@ -15,10 +17,31 @@ 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 | ||
|
|
||
| // 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 | ||
|
|
||
| // 1) The above output (3) is displayed because the variable num is declared as a constant | ||
| // 2) .slice(-1) is used to get the last digit of num inside the function. | ||
| // 3) The function does not take any parameters so when it is called with an arguments it always returns the last digit of the constant num 103. | ||
|
|
||
|
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. Would this be different if the variable was declared with
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. No. I realise that the error is actually not because it declared as const. num isn't defined as the function's parameter so the function can take arguments later on when it's called. |
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| 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)}`); | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
|
|
||
| // Now the num is passed to the function as a parameter. | ||
| // Therefore the function will return the last digit of the number when it is when it is called and passed with an argument. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,47 @@ | |
| // 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( | ||
| // p is removed from the string with the sub string method. | ||
| 0, | ||
| penceString.length - 1 | ||
| // length - 1 is used to get the index of the last character in penceString, which is p. | ||
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| // padStart is used to ensure that the string has at least 3 characters by adding 0 at the start of the string | ||
| // if it is less than 3 characters long. This is important for formatting purposes, | ||
| // as we want to ensure that we have at least 3 digits to represent pounds and pence. | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| // substring is used to extract the pounds portion of the string by taking all characters except the last two, | ||
| // which represent pence. | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
| // substring is used to extract the pence portion of the string by taking the last two characters. | ||
| // padEnd is used to ensure that the pence portion has at least 2 characters by adding 0 at the end of the string | ||
| // if it is less than 2 characters long. This is important for formatting purposes, | ||
| // as we want to ensure that we have at least 2 digits to represent pence. | ||
| return `£${pounds}.${pence}`; | ||
| // finally, the function returns a string representing the price in pounds, | ||
| // formatted with a £ symbol and a decimal point separating pounds and pence. | ||
| } | ||
| console.log(toPounds("399p")); | ||
| console.log(toPounds("1p")); | ||
| console.log(toPounds("3999p")); | ||
| // above are some test cases to check if the function works correctly for different inputs. | ||
|
|
||
| // This program takes a string representing a price in pence | ||
| // The program then builds up a string representing the price in pounds | ||
|
|
||
| // You need to do a step-by-step breakdown of each line in this program | ||
| // Try and describe the purpose / rationale behind each step | ||
|
|
||
| // To begin, we can start with | ||
| // 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
|
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. These comments are a hangover from the task in Sprint 1 (as are lots of the comments you have in the body of the function) - when copying code (and this goes for any time you do it!) make sure you are only bringing over the relevant stuff, and anything that isn't relevant should be excluded
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. Ok. Noted. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,16 +23,26 @@ function formatTimeDisplay(seconds) { | |
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
|
|
||
| // pad will be called 3 times when formatTimeDisplay is called. Once for totalHours, once for remainingMinutes and one for remainingSeconds. | ||
|
|
||
| // Call formatTimeDisplay with an input of 61, now answer the following: | ||
|
|
||
| // b) What is the value assigned to num when pad is called for the first time? | ||
| // =============> write your answer here | ||
|
|
||
| // The value assigned to num when pad is called for the first time is 0. This is because totalHours is calculated as (totalMinutes - remainingMinutes) / 60, which results in 0 when the input is 61 seconds. | ||
|
|
||
| // c) What is the return value of pad is called for the first time? | ||
| // =============> write your answer here | ||
|
|
||
| // The return value of pad when it is called for the first time is 00. this is because the value of num is 0 and the pad function adds a 0 to the front of the string until the num characters has at least 2 characters. | ||
|
|
||
|
Comment on lines
+38
to
+39
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. To be precise, what exactly is the return value of pad? I think you know what it is, but
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. Hi Liam. Thanks for reviewing my code. |
||
| // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
|
|
||
| //The value assigned to num when pad is called for the last time is 1. This is because the last call to pad is for remainingSeconds which is calculated as seconds % 60, the result of which is 1. | ||
|
|
||
| // e) What is the return value of pad when it is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
|
|
||
| // The return value of pad when it is called for the last time is 01. This is because the pad function adds a 0 to the front of the string if the characters of the string are less than 2. | ||
|
Comment on lines
+47
to
+48
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. Same as above comment!
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. Again it will be a single 0. |
||
Uh oh!
There was an error while loading. Please reload this page.
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 a great explanation of the errors, just bear in mind that the sentence:
is a matter of opinion! No need to comment on the cleanliness of the code here 🙂
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.
That is a good point. I will bear that in the mind when commenting on a code next time :)