Sheffield | ITP-Jan-26 | Hayriye Saricicek | Sprint 2 | Module Structuing and Testing Data#1186
Sheffield | ITP-Jan-26 | Hayriye Saricicek | Sprint 2 | Module Structuing and Testing Data#1186mshayriyesaricicek wants to merge 14 commits intoCodeYourFuture:mainfrom
Conversation
| // return the BMI of someone based off their weight and height | ||
| } No newline at end of file | ||
| const bmi = weight / (height * height); //calculation to calculate BMI | ||
| return bmi.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.
Thank you for pointing this out. I have researched this now and understand that the answer would have been a number if I hadn't requested it to be to 1 decimal place. The tofixed(1) converts the number to a string. In this case it doesn't effect the outcome of the answer printed but I now understand that if I did need to do calculations on the answer then it would need to be converted back to a number in order to do this so it is good practice to convert back to a number.
I have amended the program to convert the string answer back to a number and print a number.
I know as good practice there are usually inbuilt checks in case people input incorrect data. If you want me to do this I can.
There was a problem hiding this comment.
As long as you are aware of the type of the data you are returning, its is enough.
The spec was not clear on this anyway, and returning the result as a number as its shorting coming too.
| function getPenceString(penceString){ | ||
|
|
||
| const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1); | ||
| //removes p | ||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| //puts zeros in front of number if less than 3 digits | ||
| const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); | ||
| //removes last 2 digits to get pounds | ||
| const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
| //gets last 2 digits to get pence, if less than 2 digits adds zeros to end of string but shouldnt be less than 3 | ||
| //due to padStart above | ||
| return `£${pounds}.${pence}`; | ||
| // returns string with pounds and pence in correct format | ||
| } | ||
|
|
||
| console.log(getPenceString("399p")); | ||
| // tested with 123p 1200p 24589p 89p 9p and 0p |
There was a problem hiding this comment.
Indentation is off.
Have you installed prettier VSCode extension and enabled formatting on save/paste on VSCode
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?
There was a problem hiding this comment.
I did install prettier and enabled formatting on save/paste. I may have done it after I had done this or it may not be working.
There was a problem hiding this comment.
Understood.
I cannot keep track of whom I had reminded of this. So I just nagged the person with this reminder whenever I spotted improperly formatted code.
|
All good! Well done! |
Self checklist
Changelist
Coding for Sprint 2