Skip to content

Commit 28f4280

Browse files
committed
wraps sprint-1 logic in reusable function
1 parent 777e5f8 commit 28f4280

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,41 @@
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+
// const penceString = "399p";
9+
10+
// removed the hardcoded penceString and replaced with a parameter
11+
// called penceString
12+
function toPounds(penceString) { // penceString is now a parameter, not
13+
// a hardcoded variable
14+
15+
// the rest of the code remains unchanged, but it is now inside the
16+
// toPounds function, and it uses the penceString parameter instead
17+
// of the hardcoded variable.
18+
const penceStringWithoutTrailingP = penceString.substring(
19+
0,
20+
penceString.length - 1
21+
);
22+
23+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
24+
const pounds = paddedPenceNumberString.substring(
25+
0,
26+
paddedPenceNumberString.length - 2
27+
);
28+
29+
const pence = paddedPenceNumberString
30+
.substring(paddedPenceNumberString.length - 2);
31+
32+
// the old console.log statement is removed and replaced with a return
33+
// stateement that returns the formmated string with the pounds and pence.
34+
return ${pounds}.${pence}`;
35+
}
36+
37+
// console.log(`£${pounds}.${pence}`);
38+
39+
// the function toPounds can now be called with different pence strings
40+
// to test its functionality.
41+
console.log(toPounds("399p")); // £3.99
42+
console.log(toPounds("5p")); // £0.05
43+
console.log(toPounds("1234p")); // £12.34
44+

0 commit comments

Comments
 (0)