Skip to content

Commit 4366cc4

Browse files
authored
Added toPounds function for currency conversion to make a reusable block of code.
Implemented the toPounds function to convert pence to pounds.
1 parent 9c6d604 commit 4366cc4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,20 @@
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+
function toPounds(penceString) {
10+
const penceStringWithoutTrailingP = penceString.substring(
11+
0,
12+
penceString.length - 1
13+
);
14+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
15+
const pounds = paddedPenceNumberString.substring(
16+
0,
17+
paddedPenceNumberString.length - 2
18+
);
19+
const pence = paddedPenceNumberString
20+
.substring(paddedPenceNumberString.length - 2)
21+
.padEnd(2, "0");
22+
return ${pounds}.${pence}`;
23+
}

0 commit comments

Comments
 (0)