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.
| return str.toUpperCase().split('').map(function(c) { | ||
| // splits string into individual characters and maps each character to a new value | ||
|
|
||
| return /[A-Z0-9]/.test(c) ? c : '_'; | ||
| // checks if the character is an uppercase letter or a digit | ||
|
|
||
| }).join(''); | ||
| // joins the array of characters back into a single string and replaces spaces with underscores |
There was a problem hiding this comment.
The comment on line 27 and the code on lines 20-26 do not quite match because
space characters are not the only characters that will get replaced by _ in your implementation.
We can also use .replace() or .replaceAll() to achieve to same outcome -- less code to write.
There was a problem hiding this comment.
I have changed line 27 to show that it replacds special characters as well as spaces with underscore.
I have researched .replace() and .replaceAll() and see that this works on the whole string at once so wouldn't need to split the string then join it again. Thank you for the advice.
| 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.
Self checklist
Changelist
Coding for Sprint 2