Skip to content

Commit 183ee2d

Browse files
committed
Refactor program into a reusable function
1 parent 49b8d14 commit 183ee2d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

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

0 commit comments

Comments
 (0)