Skip to content

Commit 20a496c

Browse files
committed
3-madatory-implement 3-to-pounds.js completed formatted with updated code
1 parent 0d05247 commit 20a496c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,37 @@
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+
//Rewrite this code
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+
//Updated code to make it re-useable:
29+
30+
function toPounds(fromPenceString) {
31+
const penceString = fromPenceString.substring(0, fromPenceString.length -1);
32+
const paddedString = penceString.padStart(3, "0");
33+
const pounds = paddedString.substring(0, paddedString.length -2);
34+
const pence = paddedString.substring(paddedString.length -2).padEnd(2, "0");
35+
return ${pounds}.${pence}`;
36+
}
37+
console.log(toPounds("399p")); // £3.99
38+
console.log(toPounds("45p")); // £0.45
39+
console.log(toPounds("1295p")); // £12.95
40+
console.log(toPounds("5p")); // £0.05

0 commit comments

Comments
 (0)