Repository navigation
Manchester | 26-ITP-Sep | Mathew Shaibu | Sprint 3 | Coursework/sprint-3 #1587
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 2 commits
db8bec9
af89dc0
f919f28
a56fa7f
7a68faf
29644df
82f391f
da4a942
6b0f9f4
a1a3870
5e0afc9
cafd4e7
7c9837f
20a688e
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,24 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // ======> write your prediction here: I predict that the code should cut out, and make the first letter of the string an uppercase letter, | ||
| // then add every letters that comes after | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| 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; | ||
| // } | ||
|
|
||
|
|
||
| // =============> write your explanation here: The error read "SyntaxError: Identifier 'str' has already been declared", | ||
| // this is as a result of having two variables with same name. The error occoured in this code because the variable "str" has been decleared twice | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
|
|
||
| function capitalise(str) { | ||
| // let str2= `${str[0].toUpperCase()}${str.slice(1)}` | ||
| // return str2 | ||
| return str2= `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
| console.log(capitalise("money")) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,38 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // Answer below: | ||
| // A syntax error will occur for two reasons, first because there variable const "decimalNumber" being redecleared | ||
| // The second reason is because the function "convertToPercentage" is not called correctly. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
| // =============> write your prediction here: | ||
| // Answer below: | ||
| // I predicted that the code will not work because of the variable (decimalNumber) inside the function, | ||
| // Also because the console.log(decimalNumber) is just a name that exist inside the function | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| // function convertToPercentage(decimalNumber) { | ||
| // const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| console.log(decimalNumber); | ||
| // return percentage; | ||
| // } | ||
| // console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // Answer: | ||
|
|
||
| // const decimalNumber redeclares the parameter name which causes a SyntaxError. | ||
| // also console.log(decimalNumber) outside fails because the parameter only exists inside the function" | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| console.log(convertToPercentage(10.5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,31 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
| // Answer below: | ||
| // The code will not work because the placeholder variable (3) should be an actual name like "num" | ||
|
|
||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
|
|
||
| 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 | ||
| // The error message explains the error in the function parameter (3), the function expects a name parameter instead of a number. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(10)) | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,24 @@ | ||
| // Predict and explain first... | ||
| // Answer below: | ||
| // The code needs a value inside the console.log function to multiply, but no value was useed to replace the placeholder parameters | ||
| // It should be an error, no value to return | ||
|
|
||
| // =============> write your prediction here | ||
|
|
||
| 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 | ||
| // Answer below: | ||
| // The original logs the product instead of returning it, so the function call inside the template string gives undefined | ||
|
|
||
| // 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)}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // Code will not work because the paremeters should be on same line as return | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
| // 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)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // The errors in this codes is the semi-colon in front of the return and the placeholder parameters not on same line as the return | ||
| // 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 |
|---|---|---|
|
|
@@ -14,3 +14,9 @@ | |
| // You will need to come up with an appropriate name for the function | ||
| // Use the MDN string documentation to help you find a solution | ||
| // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase | ||
|
|
||
| function upperCase(str) { | ||
| const returnUpperCase = str.toUpperCase().replaceAll(" ", "_") | ||
| return returnUpperCase | ||
| } | ||
| console.log(upperCase("what is your name mr man? ")) | ||
|
Contributor
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's a safer practice to end every statement with a semicolon. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,43 @@ | |
| // 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 | ||
|
|
||
|
|
||
| // const penceString = "399p"; | ||
|
|
||
| // const penceStringWithoutTrailingP = penceString.substring( | ||
| // 0, | ||
| // penceString.length - 1 | ||
| // ); | ||
|
|
||
| // const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| // const pounds = paddedPenceNumberString.substring( | ||
| // 0, | ||
| // paddedPenceNumberString.length - 2 | ||
| // ); | ||
|
|
||
| // const pence = paddedPenceNumberString | ||
| // .substring(paddedPenceNumberString.length - 2) | ||
| // .padEnd(2, "0"); | ||
|
|
||
| // console.log(`£${pounds}.${pence}`); | ||
|
|
||
|
|
||
| function toPounds(inPounds) { | ||
| const penceStringWithoutTrailingP = inPounds.substring( | ||
| 0, | ||
| inPounds.length - 1 | ||
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
| return `£${pounds}.${pence}`; | ||
| } | ||
|
Contributor
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. Indentation is not very consistent. Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode, |
||
| console.log(toPounds("599p")) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| function pad(num) { | ||
| // console.log(num) | ||
| let numString = num.toString(); | ||
| // console.log(numString, "numstr") | ||
| while (numString.length < 2) { | ||
| numString = "0" + numString; | ||
| } | ||
| // console.log(numString, "FULLNUM") | ||
|
Contributor
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.
|
||
| return numString; | ||
| } | ||
|
|
||
|
|
||
| function formatTimeDisplay(seconds) { | ||
| const remainingSeconds = seconds % 60; | ||
| const totalMinutes = (seconds - remainingSeconds) / 60; | ||
|
|
@@ -14,25 +18,38 @@ function formatTimeDisplay(seconds) { | |
|
|
||
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
| console.log(formatTimeDisplay(61)) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit | ||
| // to help you answer these questions | ||
|
|
||
| // Questions | ||
|
|
||
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
| // =============> write your answer here | ||
| // Answer below: | ||
| // Pad runs once for "totalHours", once "remainingMinutes" and once for "remainingSeconds" in total pad is called 3 times. | ||
|
|
||
| // 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 | ||
| // =============> write your answer here | ||
| // Answer below: | ||
| // The first pad is pad(totalHours) and the value is 0 | ||
|
|
||
| // c) What is the return value of pad when it is called for the first time? | ||
| // =============> write your answer here | ||
| // The value is 0 plus the "0" added to it to make the value 00 | ||
|
Contributor
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 more precisely express a value is a string, we can enclose the value by a pair of double quotes. For example, "00". |
||
|
|
||
| // 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 | ||
| // =============> write your answer here | ||
| // Answer below: | ||
| // The last call is pad(remainingSeconds) and the value 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 | ||
| // =============> write your answer here | ||
| // Answer below: | ||
| // the return value initially is 1 and "0" was added to it in front to make the value 01 | ||
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.
What is the purpose of
str2=?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.
Thank you for reviewing my codes. I have fixed this error