Skip to content

Commit f63026f

Browse files
committed
Create reusable toPounds function from Sprint-1 code
1 parent 19f296f commit f63026f

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
// Define a function that converts kilograms to pounds
2-
function toPounds(kg) {
3-
const pounds = kg * 2.20462;
4-
return Number(pounds.toFixed(2)); // return as number, not string
1+
function toPounds(penceString) {
2+
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
3+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
4+
5+
const pounds = paddedPenceNumberString.substring(
6+
0,
7+
paddedPenceNumberString.length - 2
8+
);
9+
10+
const pence = paddedPenceNumberString
11+
.substring(paddedPenceNumberString.length - 2)
12+
.padEnd(2, "0");
13+
14+
return ${pounds}.${pence}`;
515
}
616

7-
module.exports = toPounds;
17+
// Test calls
18+
console.log(toPounds("399p")); // Output: £3.99
19+
console.log(toPounds("5p")); // Output: £0.05
20+
console.log(toPounds("45p")); // Output: £0.45
21+
console.log(toPounds("999p")); // Output: £9.99
22+
console.log(toPounds("2p")); // Output: £0.02

0 commit comments

Comments
 (0)