-
-
Notifications
You must be signed in to change notification settings - Fork 390
Birmingham | 26-ITP-May | Gabriel Pawuoi| Sprint 2 | Coursework #1467
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
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,9 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
| // Predict: SyntaxError or ReferenceError because `str` is re-declared with `let` inside the function | ||
| // where it already exists as a parameter. | ||
| // Explanation: You cannot use `let` to re-declare a variable that already exists in the same scope. | ||
| // Fix: remove the `let` keyword — just reassign str. | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,14 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // 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 | ||
| // Prediction: Two errors — (1) re-declaring decimalNumber with const inside the function | ||
| // when it is already a parameter shadows and causes a SyntaxError. | ||
| // (2) console.log(decimalNumber) outside the function will throw ReferenceError because | ||
| // decimalNumber is not defined in the outer scope. | ||
| // Explanation: The parameter decimalNumber already exists; const re-declaration is illegal. | ||
| // Also, decimalNumber is not accessible outside the function. | ||
| // Fix: remove the const re-declaration inside the function, and pass a value to the function. | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| console.log(convertToPercentage(0.5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,9 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
| // Prediction: SyntaxError — a function parameter must be a valid identifier, not a literal number. | ||
| // Error: SyntaxError: Unexpected number | ||
| // Explanation: `3` is a number literal and cannot be used as a parameter name. | ||
| // Fix: use a named parameter 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(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // =============> explain this error message here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,11 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // Prediction: The template literal will print "undefined" for the function result because | ||
| // multiply uses console.log internally but does not return a value (returns undefined). | ||
| // Explanation: Functions without a return statement return undefined. | ||
| // The outer console.log then interpolates undefined into the string. | ||
| // Fix: replace console.log inside multiply with a return statement. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // Prediction: The sum will print "undefined" because return; exits the function | ||
| // immediately without a value, so a + b is never evaluated. | ||
| // Explanation: return; with no expression returns undefined. The a + b on the next | ||
| // line is unreachable dead code. | ||
| // Fix: put a + b on the same line as return. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,17 @@ | ||
| // Predict and explain first... | ||
| // Prediction: All three logs will print "3" (the last digit of the module-level const num = 103) | ||
| // because getLastDigit ignores its parameter and always uses the outer num. | ||
| // Output: | ||
| // The last digit of 42 is 3 | ||
| // The last digit of 105 is 3 | ||
| // The last digit of 806 is 3 | ||
| // Explanation: The function has no parameter, so any argument passed in is discarded. | ||
| // It always reads the outer `num` variable which is 103. | ||
| // Fix: add a parameter to getLastDigit and use it. | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
|
|
||
| const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,6 @@ | ||
| // A set of words can be grouped together in different cases. | ||
| function toUpperSnakeCase(str) { | ||
| return str.replaceAll(" ", "_").toUpperCase(); | ||
| } | ||
|
|
||
| // For example, "hello there" in snake case would be written "hello_there" | ||
| // UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. | ||
|
|
||
| // Implement a function that: | ||
|
|
||
| // Given a string input like "hello there" | ||
| // When we call this function with the input string | ||
| // it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" | ||
|
|
||
| // Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" | ||
|
|
||
| // 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 | ||
| console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" | ||
| console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,12 @@ | ||
| // In Sprint-1, there is a program written in interpret/to-pounds.js | ||
| function toPounds(penceString) { | ||
| const withoutP = penceString.substring(0, penceString.length - 1); | ||
| const padded = withoutP.padStart(3, "0"); | ||
| const pounds = padded.substring(0, padded.length - 2); | ||
| const pence = padded.substring(padded.length - 2).padEnd(2, "0"); | ||
| return `£${pounds}.${pence}`; | ||
| } | ||
|
|
||
| // You will need to take this code and turn it into a reusable block of code. | ||
| // 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 | ||
| console.log(toPounds("399p")); // £3.99 | ||
| console.log(toPounds("9p")); // £0.09 | ||
| console.log(toPounds("50p")); // £0.50 | ||
| console.log(toPounds("1000p")); // £10.00 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,24 +15,20 @@ function formatTimeDisplay(seconds) { | |
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
|
|
||
| // 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 | ||
| // a) pad is called 3 times per call to formatTimeDisplay (once for hours, minutes, seconds). | ||
|
|
||
| // Questions | ||
| // Call formatTimeDisplay(61): | ||
| // remainingSeconds = 61 % 60 = 1 | ||
| // totalMinutes = (61 - 1) / 60 = 1 | ||
| // remainingMinutes = 1 % 60 = 1 | ||
| // totalHours = (1 - 1) / 60 = 0 | ||
| // pad is called with: totalHours=0, remainingMinutes=1, remainingSeconds=1 | ||
|
|
||
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
| // b) num = 0 (totalHours) when pad is called for the first time. | ||
|
|
||
| // Call formatTimeDisplay with an input of 61, now answer the following: | ||
| // c) pad(0): "0".length < 2, so prepend "0" -> "00". Return value: "00" | ||
|
|
||
| // b) What is the value assigned to num when pad is called for the first time? | ||
| // =============> write your answer here | ||
| // d) num = 1 (remainingSeconds) when pad is called for the last time. | ||
| // It is the last argument passed in the template literal. | ||
|
|
||
| // c) What is the return value of pad is called for the first time? | ||
| // =============> write your answer here | ||
|
|
||
| // 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 | ||
|
|
||
| // 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 | ||
| // e) pad(1): "1".length < 2, so prepend "0" -> "01". Return value: "01" | ||
|
Comment on lines
+27
to
+34
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 answers are good, but it could be a little confusing to gauge the answers mean without the context of the questions since you deleted them. This is somewhat true of the other questions as well, it's not the end of the world but bear in mind that if you were to ever look back on this repo, you'd have to look at the commits to see the diffs (i.e. what we can see right now in GitHub) in order to understand the work. 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. Note: you don't need to do anything in response to this comment, but I just wanted to highlight it for your own reference in case you should want to do anything. |
||
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.
Generally we reassign the value of a variable if we want to update the value whilst the recognising that what the value represents has stayed the same. Here, you're reassigning str to be something that represents a different thing. Considering this, can you think of an alternative approach to reassigning the existing str variable that would make this a little bit more readable?