-
-
Notifications
You must be signed in to change notification settings - Fork 299
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 all commits
7e9bb59
21acb1b
0f13007
1e5cfdb
ad4ea25
0377355
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 |
|---|---|---|
|
|
@@ -15,5 +15,11 @@ | |
| // It should return their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| let heightSquared = height * height; | ||
| let bmi = weight / heightSquared; | ||
| 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. |
||
| } | ||
|
|
||
| console.log(calculateBMI(70, 1.73)); | ||
| // Expected output: 23.4 | ||
| // Actual output: 23.4 | ||
| 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. |
||
| 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.