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+ // const penceString = "399p";
8+
9+ // const penceStringWithoutTrailingP = penceString.substring(
10+ // 0,
11+ // penceString.length - 1
12+ // );
13+
14+ // const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
15+ // const pounds = paddedPenceNumberString.substring(
16+ // 0,
17+ // paddedPenceNumberString.length - 2
18+ // );
19+
20+ // const pence = paddedPenceNumberString
21+ // .substring(paddedPenceNumberString.length - 2)
22+ // .padEnd(2, "0");
23+
24+
25+ function toPounds ( penceString ) {
26+ const penceStringWithoutTrailingP = penceString . substring ( 0 , penceString . length - 1 ) ;
27+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
28+ const pounds = paddedPenceNumberString . substring ( 0 , paddedPenceNumberString . length - 2 ) ;
29+ const pence = paddedPenceNumberString
30+ . substring ( paddedPenceNumberString . length - 2 )
31+ . padEnd ( 2 , "0" ) ;
32+ return `£${ pounds } .${ pence } ` ;
33+ }
34+ console . log ( toPounds ( "399p" ) ) ;
35+
36+ // This program takes a string representing a price in pence
37+ // The program then builds up a string representing the price in pounds
38+
39+ // You need to do a step-by-step breakdown of each line in this program
40+ // Try and describe the purpose / rationale behind each step
41+
42+ // To begin, we can start with
43+ // 1. const penceString = "399p": initialises a string variable with the value "399p"
0 commit comments