Skip to content

Commit 1c4a2db

Browse files
committed
update: Implement toPounds function to convert pence strings to formatted pounds with example usage Sprint-2/3-mandatory-implement/3-to-pounds.js
1 parent 32c94e5 commit 1c4a2db

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sprint-2/3-mandatory-implement/3-to-pounds.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,32 @@
44
// You will need to declare a function called toPounds with an appropriately named parameter.
55

66
// You should call this function a number of times to check it works for different inputs
7+
function toPounds(penceString) {
8+
// Remove the trailing "p" from the string
9+
const penceStringWithoutTrailingP = penceString.substring(
10+
0,
11+
penceString.length - 1
12+
);
13+
14+
// Ensure the number has at least 3 digits by padding from the left with zeros
15+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
16+
17+
// Extract the pound portion
18+
const pounds = paddedPenceNumberString.substring(
19+
0,
20+
paddedPenceNumberString.length - 2
21+
);
22+
23+
// Extract the pence portion and pad right if needed
24+
const pence = paddedPenceNumberString
25+
.substring(paddedPenceNumberString.length - 2)
26+
.padEnd(2, "0");
27+
28+
// Return the formatted result
29+
return ${pounds}.${pence}`;
30+
}
31+
32+
// Example usage:
33+
console.log(toPounds("399p"));
34+
console.log(toPounds("155p"));
35+
console.log(toPounds("75p"));

0 commit comments

Comments
 (0)