Skip to content

Commit 5711722

Browse files
authored
pence To Pound
1 parent 61620bc commit 5711722

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Sprint-2/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+
8+
function toPounds(penceToPound) {
9+
const penceStringWithoutTrailingP = penceToPound.substring(0, penceToPound.length - 1); // Remove the P at the end.
10+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // Make sure there 3 digits at least.
11+
const pounds = paddedPenceNumberString.substring(
12+
0,
13+
paddedPenceNumberString.length - 2
14+
); // get the pounds part( - the last two digits).
15+
const pence = paddedPenceNumberString
16+
.substring(paddedPenceNumberString.length - 2)
17+
.padEnd(2, "0"); // get the pence part( they should be 2 digits).
18+
return ${pounds}.${pence}`; // The final format.
19+
}
20+
console.log(toPounds("3990p"));
21+
console.log(toPounds("1500p"));
22+
console.log(toPounds("150p"));
23+
console.log(toPounds("100p"));
24+
console.log(toPounds("70p"));
25+
console.log(toPounds("50p"));
26+
console.log(toPounds("0p"));

0 commit comments

Comments
 (0)