Skip to content

Commit a8ef7ce

Browse files
committed
Added pence-to-pounds solution with line by line breakdown and test cases
1 parent 376454f commit a8ef7ce

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,28 @@ console.log(`£${pounds}.${pence}`);
2525

2626
// To begin, we can start with
2727
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28+
29+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1):
30+
// Removes the final "p" character so only the numeric part of the price remains
31+
32+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
33+
// Ensures the number is atleast 3 digits long by adding leading zeros when necessary, which helps with consistent formatting
34+
35+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2):
36+
// Extracts everything except the last two digits to form the pounds portion of the price
37+
38+
// 5. const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"):
39+
// Takes the final two digits of the padded string to form the pence portion
40+
41+
// 6. console.log(`£${pounds}.${pence}`):
42+
// Combines the pounds and pence into a standard currency format and prints it to the console, "£3.99"
43+
44+
// Test cases to consider:
45+
// "5p" should output "£0.05"
46+
// "99p" shsould output "£0.99"
47+
// "399p" should output "£3.99"
48+
// "0p" should output "£0.00"
49+
// "1000p" should output "£10.00"
50+
51+
// The program takes a string representing a price in pence (e.g., "399p")
52+
// and converts it to a string representing the price in pounds (e.g., "£3.99").

0 commit comments

Comments
 (0)