Skip to content

Commit 4f7798d

Browse files
committed
Solved mandatory-interpret/3-to pounds.js
1 parent 85635a5 commit 4f7798d

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ console.log(`£${pounds}.${pence}`);
2424
// Try and describe the purpose / rationale behind each step
2525

2626
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
27+
// 1. const penceString = "399p": initializes a string variable with the value "399p"
28+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a copy of the penceString and method substring() removes the last character 'p', using the length of the original string minus 1 as the endpoint
29+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): ensures that the string has at least 3 characters, if not - adds zeros to the start of the string until it reaches that length. Method padStart() is used for this purpose - padStart(targetLength, padString), where targetLength is 3 and padString is "0". If string is already 3 or more characters long, no symbol is added.
30+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds from the paddedPenceNumberString by taking all characters except the last two, using substring() method with the endpoint set to the length of the string minus 2
31+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the last two characters with method substring() from the paddedPenceNumberString to represent the pence (starting index set to the length of the string minus 2). Then, padEnd(2, "0") ensures that the resulting string has at least 2 characters, adding a trailing zero if necessary.
32+
// 6. console.log(`£${pounds}.${pence}`): outputs the formatted amount of money/price in pounds and pence to the console, using template literals for nice readable view (it allowed combining the pounds and pence with the appropriate currency symbol and decimal point.

0 commit comments

Comments
 (0)