Skip to content

Commit f4d3f59

Browse files
committed
Refactor comments for clarity and remove redundant explanations in 3-to-pounds.js
1 parent 5f6ff93 commit f4d3f59

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

Sprint-1/3-mandatory-interpret/3-to-pounds.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ const pence = paddedPenceNumberString
1717

1818
console.log(${pounds}.${pence}`);
1919

20-
// This program takes a string representing a price in pence
21-
// The program then builds up a string representing the price in pounds
22-
23-
// You need to do a step-by-step breakdown of each line in this program
24-
// Try and describe the purpose / rationale behind each step
25-
26-
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
20+
// 1. const penceString = "399p": initialises a string variable with the value "399p".
21+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): removes the trailing "p" from the string by taking all characters from index 0 up to but not including the last one, leaving "399". This isolates the numeric part of the price.
22+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): ensures the numeric string has at least three digits by adding leading zeros if needed (for example, "5p" becomes "005"). This makes it easier to separate pounds and pence later.
23+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts all digits except the last two to represent the pounds. For "399" this gives "3".
24+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): takes the last two digits of the string as pence, and if needed, pads the end with a zero to ensure two digits. For "399" this gives "99".
25+
// 6. console.log(`£${pounds}.${pence}`): prints the formatted price as a string in the style of "£3.99". The program as a whole converts a pence-based string like "399p" into a properly formatted pounds and pence display.

0 commit comments

Comments
 (0)