Skip to content

Commit 4cb4a65

Browse files
committed
implement toPounds function to format pence strings
1 parent dde33c9 commit 4cb4a65

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,28 @@
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+
const penceStringWithoutTrailingP = penceString.substring(
9+
0,
10+
penceString.length - 1
11+
);
12+
13+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
14+
15+
const pounds = paddedPenceNumberString.substring(
16+
0,
17+
paddedPenceNumberString.length - 2
18+
);
19+
20+
const pence = paddedPenceNumberString
21+
.substring(paddedPenceNumberString.length - 2)
22+
.padEnd(2, "0");
23+
24+
return ${pounds}.${pence}`;
25+
}
26+
27+
// test calls
28+
console.log(toPounds("399p")); // £3.99
29+
console.log(toPounds("50p")); // £0.50
30+
console.log(toPounds("5p")); // £0.05
31+
console.log(toPounds("100p")); // £1.00

0 commit comments

Comments
 (0)