London | 26-ITP-May | Raihan Sharif | Sprint 2 | Coursework#1303
London | 26-ITP-May | Raihan Sharif | Sprint 2 | Coursework#1303RaihanSharif wants to merge 7 commits intoCodeYourFuture:mainfrom
Conversation
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } No newline at end of file | ||
| return (weight / height ** 2).toFixed(1); |
There was a problem hiding this comment.
What type of value do you expect your function to return? A number or a string?
Does your function return the type of value you expect?
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.There was a problem hiding this comment.
I have added type conversion to cast string to Number, or return NaN if not possible to cast.
cast to Number, or return NaN if not posible to do so.
|
|
||
| function calculateBMI(weight, height) { | ||
| return (weight / height ** 2).toFixed(1); | ||
| return (Number(weight) / Number(height) ** 2).toFixed(1); |
There was a problem hiding this comment.
How you checked if the return value is a number and not a string after you made the changes?
There was a problem hiding this comment.
I did not. Now it's fixed.
Check the input, convert to Number if possible, else return NaN. Output is a Number.
|
All good now. |
Learners, PR Template
Self checklist
Changelist
Tasks done as per spec. Ran the code to see if it works.