-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | ITP-JAN-2026 | Said Fayaz Sadat | Sprint 2 | coursework/sprint 2 #1059
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 5 commits
2a0a2b7
bb7033c
8ef3eaf
d64b734
1188e3e
7d24e58
bd6a95d
a1ae7a3
dd75584
bc7784e
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,22 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // An error will occur because the function 'multiply' does not return any value. When we try to use the result of 'multiply(10, 32)' in the template literal, it will be 'undefined', which is not the expected output. To fix this, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'. | ||
|
|
||
| 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)}`); | ||
| */ | ||
|
|
||
| // =============> write your explanation here | ||
| // The error is occurring because the 'multiply' function does not return any value. When we call 'multiply(10, 32)', it executes the console.log statement inside the function, which prints the product of 'a' and 'b' to the console. However, since there is no return statement, the function returns 'undefined' by default. When we try to use this 'undefined' value in the template literal, it does not give us the expected output. To fix this issue, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'. | ||
|
|
||
| // 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 | ||
| // An error will occur because the 'sum' function does not return any value. When we try to use the result of 'sum(10, 32)' in the template literal, it will be 'undefined', which is not the expected output. To fix this, we need to add a return statement in the 'sum' function to return the sum of 'a' and 'b'. | ||
|
|
||
| function sum(a, b) { | ||
| /* function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
|
|
||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| */ | ||
|
|
||
| // =============> write your explanation here | ||
| // The error is occurring because the 'sum' function does not return any value. When we call 'sum(10, 32)', it executes the return statement without any value, which means the function returns 'undefined' by default. When we try to use this 'undefined' value in the template literal, it does not give us the expected output. To fix this issue, we need to change the return statement to return the sum of 'a' and 'b', like this: 'return a + b;'. | ||
| // 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 |
|---|---|---|
|
|
@@ -16,4 +16,7 @@ | |
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| let bmi = weight / (height * height); | ||
| return bmi.toFixed(1); | ||
|
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. What type of value do you expect your function to return? A number or a string? Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example, console.log(123); // Output 123
console.log("123"); // Output 123
// Treated differently in the program
let sum1 = 123 + 100; // Evaluate to 223 -- a number
let sum 2 = "123" + 100; // Evaluate to "123100" -- a string.
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. Thanks for the feedback, My function expected a number but I noticed that toFixed() returns a string, so my function was returning a string instead of a number. I updated the return to Number(bmi.toFixed(1)) so the function now returns a number while still rounding to one decimal place. |
||
| } | ||
| console.log(calculateBMI(80, 1.80)); // should return 24.7 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,10 @@ | |
| // 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 | ||
|
|
||
| function toPounds(kilograms) { | ||
| return +(kilograms * 2.20462).toFixed(1); | ||
| } | ||
|
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. The goal is to implement the code in
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. Appreciate it for point me out in right direction, I’ve updated the implementation so the toPounds function now accepts a pence string (e.g. "399p"), converts it to pounds, and returns the correctly formatted pound value as a string. I also tested it with multiple inputs to confirm it works as expected. |
||
| console.log(toPounds(1)); // should return 2.2 | ||
| console.log(toPounds(5)); // should return 11 | ||
| console.log(toPounds(10)); // should return 22 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,20 @@ | |
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| 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. We could also extract the last 2 characters as
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. changes it to time.slice(-2) to get the characters. |
||
|
|
||
| if (hours === 0) { | ||
| return `12:${minutes} am`; | ||
| } | ||
|
|
||
| if (hours === 12) { | ||
| return `12:${minutes} pm`; | ||
| } | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:${minutes} pm`; | ||
| } | ||
|
|
||
| return `${time} am`; | ||
|
Comment on lines
+17
to
21
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 probably reasonable for the caller to expect "13:00" to be converted to "01:00 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.
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. Thanks for the feedback Yuan. I see what you mean about the formatting inconsistency. I updated the function so that the hour is always returned with two digits using padStart, ensuring values like "13:00" are converted to "01:00 pm" to match the "01:00 am" format. |
||
| } | ||
|
|
||
|
|
@@ -23,3 +34,24 @@ 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("13:00"); | ||
| const targetOutput5 = "1:00 pm"; | ||
| console.assert( | ||
| currentOutput5 === targetOutput5, | ||
| `current output: ${currentOutput5}, target output: ${targetOutput5}` | ||
| ); | ||
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.
It is better to break a long line of comment or code into multiple lines so that one doesn't have to scroll horizontally in an editor to read everything.
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.
To be honest I didn't thought of that, fixed that one and going forward i will keep that in mind. Thanks.