Skip to content

Commit 2b746b1

Browse files
committed
Different display of pounds
1 parent 9577ac5 commit 2b746b1

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,56 @@
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+
let penceString = "399p";
10+
11+
let penceStringWithoutTrailingP = penceString.substring(
12+
0,
13+
penceString.length - 1
14+
);
15+
16+
let paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
17+
let pounds = paddedPenceNumberString.substring(
18+
0,
19+
paddedPenceNumberString.length - 2
20+
);
21+
22+
let pence = paddedPenceNumberString
23+
.substring(paddedPenceNumberString.length - 2)
24+
.padEnd(2, "0");
25+
26+
console.log(`£${pounds}.${pence}`);
27+
*/
28+
29+
// Function to convert a pence string (like "399p") to pounds
30+
function toPounds(penceString) {
31+
// Remove the trailing 'p'
32+
let penceStringWithoutTrailingP = penceString.substring(
33+
0,
34+
penceString.length - 1
35+
);
36+
37+
// Pad the string to ensure at least 3 digits
38+
let paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
39+
40+
// Extract pounds
41+
let pounds = paddedPenceNumberString.substring(
42+
0,
43+
paddedPenceNumberString.length - 2
44+
);
45+
46+
// Extract pence
47+
let pence = paddedPenceNumberString
48+
.substring(paddedPenceNumberString.length - 2)
49+
.padEnd(2, "0");
50+
51+
return ${pounds}.${pence}`;
52+
}
53+
54+
// Example usage:
55+
console.log(toPounds("399p")); // Output: £3.99
56+
console.log(toPounds("5p")); // Output: £0.05
57+
console.log(toPounds("1234p")); // Output: £12.34
58+
console.log(toPounds("50p")); // Output: £0.50
59+

0 commit comments

Comments
 (0)