Skip to content

Commit 2baf879

Browse files
turn to pound code to reusable code
1 parent 9cce8a7 commit 2baf879

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,23 @@
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 toPound(penceString) {
8+
const penceStringWithoutTrailingP = penceString.substring(
9+
0,
10+
penceString.length - 1,
11+
);
12+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
13+
const pounds = paddedPenceNumberString.substring(
14+
0,
15+
paddedPenceNumberString.length - 2,
16+
);
17+
const pence = paddedPenceNumberString
18+
.substring(paddedPenceNumberString.length - 2)
19+
.padEnd(2, "0");
20+
return `£${pounds}.${pence}`;
21+
}
22+
console.log(toPound("399p"));
23+
console.log(toPound("2p"));
24+
console.log(toPound("4500p"));
25+
console.log(toPound("95p"));
26+
console.log(toPound("3675p"));

0 commit comments

Comments
 (0)