-
-
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. 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. That's correct, and the point I want to make is that
Highlights that there was no "error message" such as a |
||
|
|
||
| // 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. 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! You should amend the explanation so it is correct 🙂 |
||
| // 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 |
|---|---|---|
|
|
@@ -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. 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. Not quite - what data type is the return value of |
||
| // 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. 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. See above comment 🙂 |
||
Uh oh!
There was an error while loading. Please reload this page.