-
-
Notifications
You must be signed in to change notification settings - Fork 382
Glasgow | 26-ITP-May | Niangh Ciang | Sprint 2 | Coursework #1367
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
79b6095
102a561
f98987f
d460d91
59aa251
ff7893d
03cf7b4
201e547
d0032b7
0e13a1a
5429088
3d87510
274d196
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,22 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| //I predict this code will take the first letter of the string and capitalise it. | ||
|
|
||
| // 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; | ||
| } | ||
| }*/ | ||
|
|
||
| // =============> write your explanation here | ||
| //The syntax error happens because variable str is declared twice. | ||
|
|
||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitalisedStr; | ||
| } | ||
| console.log(capitalise("hello")); | ||
| console.log(capitalise("how are you?")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,25 @@ | ||
|
|
||
| // 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 | ||
| //I think the problem in this code is the parameter inside the function. A number cannot be used as a parameter name. | ||
|
|
||
| function square(3) { | ||
| /*function square(3) { | ||
| return num * num; | ||
| } | ||
| }*/ | ||
|
|
||
| // =============> write the error message here | ||
| //SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| //The error message means that number cannot be used as a parameter name. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(2)); | ||
| console.log(square(3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,22 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| //I think this code will not show the correct result because the function logs the answer instead of returning it. | ||
|
|
||
| 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 | ||
| /*First, the function multiplies 10 and 32 and prints the result. However, in the second console.log | ||
| the value becomes undefined because the function does not return anything, so the template string | ||
| receives undefined instead of the number.*/ | ||
|
|
||
| // 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,22 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| //I think this code will not give correct output, it will give undefined. | ||
|
|
||
| function sum(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 | ||
| /*This code will print undefined because return; stops the function immediately. | ||
| The line a + b is never executed, so the function returns undefined and the template | ||
| string receives undefined instead of the sum.*/ | ||
|
|
||
| // 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 |
|---|---|---|
|
|
@@ -4,8 +4,15 @@ | |
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3, 5); | ||
|
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.
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${hours - 12}:${minutes} pm`; | ||
| } | ||
| if (hours === 12) { | ||
| return `12:${minutes} pm`; | ||
| } | ||
| if (hours === 0) { | ||
| return `12:${minutes} am`; | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
|
@@ -23,3 +30,39 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
| const currentOutput3 = formatAs12HourClock("12:00"); | ||
| const targetOutput3 = "12:00 pm"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
| ); | ||
| const currentOutput4 = formatAs12HourClock("00:00"); | ||
| const targetOutput4 = "12:00 am"; | ||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}` | ||
| ); | ||
| const currentOutput5 = formatAs12HourClock("00:30"); | ||
| const targetOutput5 = "12:30 am"; | ||
| console.assert( | ||
| currentOutput5 === targetOutput5, | ||
| `current output: ${currentOutput5}, target output: ${targetOutput5}` | ||
| ); | ||
| const currentOutput6 = formatAs12HourClock("12:35"); | ||
| const targetOutput6 = "12:35 pm"; | ||
| console.assert( | ||
| currentOutput6 === targetOutput6, | ||
| `current output: ${currentOutput6}, target output: ${targetOutput6}` | ||
| ); | ||
| const currentOutput7 = formatAs12HourClock("01:01"); | ||
| const targetOutput7 = "01:01 am"; | ||
| console.assert( | ||
| currentOutput7 === targetOutput7, | ||
| `current output: ${currentOutput7}, target output: ${targetOutput7}` | ||
| ); | ||
| const currentOutput8 = formatAs12HourClock("13:01"); | ||
| const targetOutput8 = "1:01 pm"; | ||
| console.assert( | ||
| currentOutput8 === targetOutput8, | ||
| `current output: ${currentOutput8}, target output: ${targetOutput8}` | ||
| ); | ||
|
Comment on lines
+57
to
+68
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. If "01:01" is converted to "01:01 am", it is probably reasonable for the caller to expect "13:01" to be converted to "01:01 pm". When the returned values are not formatted consistently, it may result in unintended side-effect. For examples,
Consistency is important so the caller can be certain what to expect from a function. |
||
Uh oh!
There was an error while loading. Please reload this page.