Skip to content

Commit b8a684c

Browse files
committed
Convert pence to pound function
1 parent 491605b commit b8a684c

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,42 @@
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+
// const penceString = "399p";
10+
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+
// console.log(`£${pounds}.${pence}`);
27+
28+
function convertPenceToPounds(penceString){
29+
30+
const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
31+
32+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
33+
34+
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
35+
36+
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
37+
return ${pounds}.${pence}`;
38+
}
39+
console.log(convertPenceToPounds("299p"));
40+
console.log(convertPenceToPounds("499p"));
41+
console.log(convertPenceToPounds("25p"));
42+
console.log(convertPenceToPounds("79p"));
43+
44+
45+

0 commit comments

Comments
 (0)