-
-
Notifications
You must be signed in to change notification settings - Fork 273
London | 25-ITP-Sep | Zadri Abdule | Sprint 2 | Coursework #867
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 12 commits
0fc35ed
f4e38d3
8a162e3
2bf3487
54ca144
ff14721
97dc683
2f8d74e
c52e087
0de1e19
a451eda
083a9d2
f4c8d85
2776d16
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,14 +1,20 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
|
|
||
| // It will show a undefined error | ||
| 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 printed the answer but did't return it, so when it was used in the template string, it came out as 'undefined.' | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function multiply(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,6 @@ | |
| // 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 | ||
| const greeting = "Hello there." | ||
|
|
||
| console.log(greeting.toUpperCase()); | ||
|
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. This approach will work, but this task asks you to create a function. Can you create a named function that takes a string and returns it in uppercase? Making a named function is important as it makes it easier to test code than using console.log()s |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| // 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) { | ||
| /*function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
|
|
@@ -22,4 +22,55 @@ const targetOutput2 = "11:00 pm"; | |
| console.assert( | ||
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); */ | ||
|
|
||
| // The function ignores the minutes and did'not handle midnight (00:00) or noon (12:00) properly. | ||
| // Better version: | ||
| function formatAs12HourClock(time) { | ||
|
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. Good work attempting this stretch task! If we look at your tests, you try 08:00 which gets returned as 08:00 am, and trying 13:00 gives you 1:00 pm. There seems to be a difference in how am/pm handle leading 0s. Can you find why that happens? |
||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3, 5); | ||
|
|
||
| if (hours == 0) return `12:${minutes} am`; | ||
| if (hours < 12) return `${time} am`; | ||
| if (hours === 12) return `${time} pm`; | ||
|
|
||
| return `${hours - 12}:${minutes} pm`; | ||
| } | ||
| const currentOutput1 = formatAs12HourClock("08:00"); | ||
| const targetOutput1 = "08:00 am"; | ||
| console.assert( | ||
| currentOutput1 === targetOutput1, | ||
| `current output: ${currentOutput1}, target output: ${targetOutput1}` | ||
| ); | ||
| // Test failed: due to a typo | ||
| const currentOutput2 = formatAs12HourClock("23:00"); | ||
| const targetOutput2 = "11:00 pm"; | ||
| console.assert( | ||
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
|
|
||
| ); | ||
|
|
||
| const currentOutput3 = formatAs12HourClock("13:00"); | ||
| const targetOutput3 = "1:00 pm"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3} target output: {targetOutput3}` | ||
| ); | ||
|
|
||
| const currentOutput4 = formatAs12HourClock("15:00"); | ||
| const targetOutput4 = "3:00 pm"; | ||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4} target output: {targetOutput4}` | ||
| ); | ||
|
|
||
| console.log(formatAs12HourClock("08:00")); | ||
| console.log(formatAs12HourClock("23:00")); | ||
| console.log(formatAs12HourClock("15:00")); | ||
| console.assert(formatAs12HourClock("00:00") === "12:00 am"); | ||
| console.assert(formatAs12HourClock("12:00") === "12:00 pm"); | ||
|
|
||
| // The test passed, so my function gave the expected results. | ||
| // All the console.assert() tests passed. | ||
| // formatAs12HourClock() function works correctly for the inputs I tested. | ||
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.
Here, I think the function inside the
${}has the wrong name, so this will give an error