Skip to content

Commit b4c62e1

Browse files
committed
Solved task 3-mandatory-implement/3-to-pounds.js
1 parent 0606e40 commit b4c62e1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,26 @@
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(penceStr) {
9+
const penceStrWithoutTrailingP = penceStr.substring(0, penceStr.length - 1);
10+
const paddedPenceNumberString = penceStrWithoutTrailingP.padStart(3, "0");
11+
const pounds = paddedPenceNumberString.substring(
12+
0,
13+
paddedPenceNumberString.length - 2
14+
);
15+
const pence = paddedPenceNumberString
16+
.substring(paddedPenceNumberString.length - 2)
17+
.padEnd(2, "0");
18+
19+
return ${pounds}.${pence}`;
20+
}
21+
22+
console.log(toPounds("123p")); // £1.23
23+
console.log(toPounds("1002p")); // £10.02
24+
console.log(toPounds("0p")); // £0.00
25+
console.log(toPounds("99p")); // £0.99
26+
console.log(toPounds("250p")); // £2.50
27+
console.log(toPounds("7p")); // £0.07
28+
console.log(toPounds("48p")); // £0.48
29+
console.log(toPounds("53647868p")); // £536478.68

0 commit comments

Comments
 (0)