-
-
Notifications
You must be signed in to change notification settings - Fork 337
Birmingham | 26-ITP-Jan | Arun Kumar Akilan | Sprint 2 | Structuring-and-Testing-Data Coursework/Sprint2 #1117
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
Changes from 1 commit
ab3d116
76d0588
ff2c4c5
f98c27d
52a1b76
5cf8a5a
0422ff0
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,44 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // The code has a syntax error because a number is used instead of a parameter name in the function definition. | ||
| // Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError. | ||
| // If the function is not called, it will not execute. | ||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
|
|
||
| // The code has a syntax error because a number is used instead of a parameter name in the function definition. | ||
| // Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError. | ||
| // If the function is not called, it will not execute. | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // =============> write the error message here | ||
| // syntax error :unexpected number. | ||
|
|
||
| // =============> explain this error message here | ||
| // The error occurs because a number is used in the function definition instead of a parameter name. A function definition must contain a parameter (a variable name), not a value. | ||
|
|
||
| // Inside the function, num is used but it was never declared or passed as a parameter, which causes a ReferenceError because num is not defined in the scope. | ||
|
|
||
| // Also, if the function is not called, it will not execute. | ||
|
arunkumarakilan marked this conversation as resolved.
|
||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // I corrected the function by properly defining num as a parameter instead of using a number in the function definition. This resolved the syntax error. | ||
|
|
||
| // Inside the function, num is now defined, so the ReferenceError is fixed. | ||
|
|
||
| // I then called the function with an argument and stored the returned value in a variable named num. Although the same variable name is used, the function parameter and the outer variable exist in different scopes, so there is no conflict. | ||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| let num = square(3); | ||
|
arunkumarakilan marked this conversation as resolved.
Outdated
|
||
| console.log(num); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,39 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // The function multiplies the numbers and prints the answer. | ||
|
|
||
| // But it does not return the answer. | ||
|
|
||
| // Because there is no return, the function automatically gives back undefined. | ||
|
|
||
| // So when we use the function inside the second console.log, the value is undefined. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
|
|
||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // console.log and return are not the same. | ||
|
|
||
| // console.log only prints the value to the console. | ||
|
|
||
| // It does not send the value back to the function call. | ||
|
|
||
| // Because there was no return statement, the function automatically returned undefined. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // To fix the issue, I replaced console.log with return. | ||
|
|
||
| // By returning a * b, the function now sends the calculated value back to where it was called. | ||
|
|
||
| // As a result, the correct value is displayed instead of undefined. | ||
| // =============> write your new code here | ||
| function multiply(a, b) { | ||
| return (a * b); | ||
|
arunkumarakilan marked this conversation as resolved.
Outdated
|
||
|
|
||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,8 @@ | |
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| time = hours - 12; | ||
|
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. In your new logic, you assigned the calculated number to time. While JavaScript allows this, time was originally our input string (e.g., '19:00'). Reassigning parameters to completely different data types can sometimes lead to confusing bugs later on. Can you think of a better way to store your calculated hours - 12 without overwriting the time parameter? Perhaps a new variable name? 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. @arunkumarakilan can you please reply to the reviewer? This has been waiting for 4 days now and you should reply asap. |
||
| return `${time.toString().padStart(2 , "0")}:00 pm` | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
|
@@ -23,3 +24,24 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
| const currentOutput3 = formatAs12HourClock("19:00"); | ||
| const targetOutput3 = "07:00 pm"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
| ); | ||
|
|
||
| const currentOutput4 = formatAs12HourClock("21:00"); | ||
| const targetOutput4 = "09:00 pm"; | ||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}` | ||
| ); | ||
|
|
||
|
|
||
|
|
||
| // The previous version only worked properly for two-digit hours. To fix this, I made sure the hour is always displayed in two-digit format. | ||
|
|
||
| // First, I completed the mathematical calculation and stored the result in a variable. Then, I converted it to a string and used padStart(2, "0") to add a leading zero when needed. | ||
|
|
||
| // Now, it works correctly for all times greater than 12 and always shows two digits. | ||
Uh oh!
There was an error while loading. Please reload this page.