-
-
Notifications
You must be signed in to change notification settings - Fork 239
West Midlands | 25-ITP-Sept | Ali Naru | Sprint 2 | Coursework/sprint-2 #840
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
c9f5c3b
8d94e35
b857592
fc492bb
efcb14e
15ab890
ab8b7e0
183aa5b
13ffd18
c99c22e
5a85a7a
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,37 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // I predict that the code will throw a ReferenceError because the variable 'num' is not defined within the function scope. | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // | ||
|
|
||
| // =============> write your prediction of the error here | ||
|
|
||
| // I predict that the code will throw a ReferenceError because the variable 'num' is not defined within the function scope. | ||
|
|
||
| / | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // ReferenceError: num is not defined | ||
|
|
||
| // =============> explain this error message here | ||
|
|
||
| // This error message indicates that the variable 'num' is being used before it has been declared or defined. | ||
| // In the function 'square', the parameter is incorrectly defined as a literal value (3) instead of a variable name. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| // Example: | ||
| console.log(square(3)); // Output: 9 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,25 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| // I predict that the code will output 'undefined' because the function sum does not return any value. | ||
|
|
||
| // this function should sum two numbers and return the result | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
|
|
||
| // The function sum correctly returns the sum of the two numbers, so the template literal will output the correct result. | ||
|
|
||
| // 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)}`); // No change needed as the code is already correct |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,27 @@ | |
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| } | ||
|
|
||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // I predict that the code will output 'undefined' because the function calculateBMI | ||
| // does not return any value yet. | ||
|
|
||
| // =============> write your explanation here | ||
| // The function calculateBMI currently has no return statement, so it will return 'undefined'. | ||
| // To fix this, we need to perform the BMI calculation: | ||
| // BMI = weight / (height * height) | ||
| // Then, we round the result to one decimal place using toFixed(1). | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| const 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. |
||
| } | ||
|
|
||
| // Example: | ||
| console.log(`The BMI of someone who weighs 70kg and is 1.73m tall is ${calculateBMI(70, 1.73)}`); | ||
| // Output: The BMI of someone who weighs 70kg and is 1.73m tall is 23.4 | ||
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.