-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | 26-ITP-Jan | Abdul Moiz | Sprint 2 | Sprint 2 #911
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 6 commits
7e9bb59
21acb1b
0f13007
1e5cfdb
ad4ea25
0377355
dfd2491
f02f5ff
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,20 +1,26 @@ | ||
|
|
||
| // 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 | ||
| // Prediction of the error is that the function parameter is not valid. | ||
| // It should be a variable name, not a number. | ||
|
|
||
| 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 indicates that there is an unexpected number in the function parameter list. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
|
|
||
| console.log(square(4)); // should print 16, does display correctly. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,22 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // The function multiply does not return any value, it only logs the result to the console. | ||
|
|
||
| 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 | ||
| // The function multiply does not return any value, it only logs the result to the console. | ||
| // When we try to use the result of multiply(10, 32), it will be 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 | ||
| // The function sum does not return any value because of the misplaced semicolon after 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 function sum has a return statement followed by a semicolon, which causes the function to return undefined. | ||
|
|
||
| // 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 |
|---|---|---|
|
|
@@ -2,11 +2,31 @@ | |
| // Make sure to do the prep before you do the coursework | ||
| // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. | ||
|
|
||
| // function formatAs12HourClock(time) { | ||
| // const hours = Number(time.slice(0, 2)); | ||
| // if (hours > 12) { | ||
| // return `${hours - 12}:00 pm`; | ||
| // } | ||
| // return `${time} am`; | ||
| // } | ||
|
|
||
| // Updated function: | ||
| 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. Can also consider |
||
|
|
||
| if (hours === 0) { | ||
| return `12:${minutes} am`; | ||
| } | ||
|
|
||
| if (hours === 12) { | ||
| return `12:${minutes} pm`; | ||
| } | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${hours - 12}:${minutes} pm`; | ||
| } | ||
|
|
||
| return `${time} am`; | ||
| } | ||
|
|
||
|
|
@@ -23,3 +43,20 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
|
|
||
| // Morning times | ||
| console.assert(formatAs12HourClock("01:00") === "01:00 am"); | ||
| console.assert(formatAs12HourClock("11:59") === "11:59 am"); | ||
|
|
||
| // Noon | ||
| console.assert(formatAs12HourClock("12:00") === "12:00 pm"); | ||
| console.assert(formatAs12HourClock("12:30") === "12:30 pm"); | ||
|
|
||
| // Afternoon / evening | ||
| console.assert(formatAs12HourClock("13:00") === "1:00 pm"); | ||
|
Comment on lines
+48
to
+56
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. Output on lines 56 and 48 have slightly different format.
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 I'm unsure of the question you're asking. Line 56 tests if 13:00 is same as 1:00pm and line 48 tests if 01:00 is same as 01:00 am. All the assertions are passing currently.
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:00" is converted to "01:00 am", it is reasonable for the caller to expect "13:00" to be converted to "01:00 pm". What could go wrong? Here are two examples,
01:00 am
1:00 pm
12:00 am
01:00 pm
Consistency is important so the caller can be certain what to expect from a function. Did you choose the format "1:00 pm" by design (before you implement the function), or did you set the expected value in your tests because you knew that's what your function will return? |
||
| console.assert(formatAs12HourClock("18:45") === "6:45 pm"); | ||
| console.assert(formatAs12HourClock("23:59") === "11:59 pm"); | ||
|
|
||
| // Midnight | ||
| console.assert(formatAs12HourClock("00:00") === "12:00 am"); | ||
| console.assert(formatAs12HourClock("00:15") === "12:15 am"); | ||
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.
For a variable not needed to be reassigned a value, a better practice is to use
constinstead oflet.