Skip to content

Commit 7c3a6c5

Browse files
Implement toPence function to convert pence strings to formatted pounds
1 parent d4e48c6 commit 7c3a6c5

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

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

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

0 commit comments

Comments
 (0)