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+ // In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js
9+
10+ // You will need to take this code and turn it into a reusable block of code.
11+ // You will need to declare a function called toPounds with an appropriately named parameter.
12+
13+ // You should call this function a number of times to check it works for different inputs
14+
15+
16+ /*
17+ const penceString = "399p";
18+
19+ const penceStringWithoutTrailingP = penceString.substring(
20+ 0,
21+ penceString.length - 1
22+ );
23+
24+ const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
25+ const pounds = paddedPenceNumberString.substring(
26+ 0,
27+ paddedPenceNumberString.length - 2
28+ );
29+
30+ const pence = paddedPenceNumberString
31+ .substring(paddedPenceNumberString.length - 2)
32+ .padEnd(2, "0");
33+
34+ console.log(`£${pounds}.${pence}`);
35+ */
36+
37+ function toPounds ( penceString ) {
38+ let noP = penceString . substring ( 0 , penceString . length - 1 )
39+ let paddedP = noP . padStart ( 3 , "0" )
40+ let pounds = paddedP . substring ( 0 , paddedP . length - 2 )
41+
42+ let pence = paddedP . substring ( paddedP . length - 2 ) . padEnd ( 2 , "0" )
43+
44+ return `£${ pounds } .${ pence } `
45+ }
46+ console . log ( toPounds ( "399p" ) )
47+ console . log ( toPounds ( "5p" ) )
48+ console . log ( toPounds ( "52p" ) )
49+ console . log ( toPounds ( "1244p" ) )
0 commit comments