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+ // const penceStringWithoutTrailingP = penceString.substring(
11+ // 0,
12+ // penceString.length - 1
13+ // );
14+
15+ // const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
16+ // const pounds = paddedPenceNumberString.substring(
17+ // 0,
18+ // paddedPenceNumberString.length - 2
19+ // );
20+
21+ // const pence = paddedPenceNumberString
22+ // .substring(paddedPenceNumberString.length - 2)
23+ // .padEnd(2, "0");
24+
25+ // console.log(`£${pounds}.${pence}`);
26+
27+ function poundsAndPence ( penceString ) {
28+ const penceStringWithoutTrailingP = penceString . substring (
29+ 0 ,
30+ penceString . length - 1
31+ ) ;
32+
33+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
34+ const pounds = paddedPenceNumberString . substring (
35+ 0 ,
36+ paddedPenceNumberString . length - 2
37+ ) ;
38+
39+ const pence = paddedPenceNumberString
40+ . substring ( paddedPenceNumberString . length - 2 )
41+ . padEnd ( 2 , "0" ) ;
42+
43+ return ( `£${ pounds } .${ pence } ` ) ;
44+ }
45+
46+ console . log ( poundsAndPence ( "399p" ) )
47+ console . log ( poundsAndPence ( "599p" ) )
48+ console . log ( poundsAndPence ( "1796p" ) )
49+ console . log ( poundsAndPence ( "562p" ) )
50+ console . log ( poundsAndPence ( "2p" ) )
51+ // const penceString = "399p";
0 commit comments