London | 26-ITP-January | Eugenie Ahangama | Sprint 2 | coursework/sprint 2#931
London | 26-ITP-January | Eugenie Ahangama | Sprint 2 | coursework/sprint 2#931Eugenie-A wants to merge 12 commits intoCodeYourFuture:mainfrom
Conversation
… verified correct output
…added explanation
…s to verify output
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } No newline at end of file | ||
| return (weight / (height * height)).toFixed(1); // toFixed(1) rounds to 1 decimal place. |
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 expected the function to return a number, not a string.
No, my function didn't return the type of value I was expecting it to, but rather it was returned as a string due to .toFixed(1).
I updated it to return Number( (weight / (height * height)).toFixed(1) ); - This keeps the rounding to 1 decimal place but converts back to a number.
I do not understand what you mean. Can you elaborate? |
I'm trying to get better at debugging JavaScript code in a consistent, step by step way especially when fixing broken functions or understanding why something fails. So what I'm asking is what are some reliable structured debugging habits or checklists that experienced developers use? |
|
The key of debugging is to find out which expression does not evaluate to the expected value or which variable does not hold the expected value. For small amount of code, we can output the value of suspected expressions or variables. |
Learners, PR Template
Self checklist
Changelist
I worked through the Sprint 2 exercises by going through the starter code, identifying the main errors and making sure I understood why the logic wasn't working. I then wrote tests in the debug section to clearly show the issues before fixing anything. After that, I implemented the correct JavaScript behaviour so the function produced the right results. Finally, in the interpret task, I explained what the original code was doing, why it failed and how my fixes solved those problems.
MDN References I used:
String.prototype.slice()
Number() constructor
Template literals
console.assert()
String.prototype.split()
Array.prototype.map()
Remainder operator (%)
String.prototype.padStart()
String.prototype.substring()
String.prototype.replace()
String.prototype.trim()
String.prototype.toUpperCase()
Function parameters
Return
Questions
What's the best way to structure a debugging process?