File tree Expand file tree Collapse file tree
Sprint-2/3-mandatory-implement Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ function toPounds ( str )
9+ {
10+ // 1. const penceString = "399p": initialises a string variable with the value "399p"
11+ const penceStringWithoutTrailingP = str . substring (
12+ 0 ,
13+ str . length - 1
14+ ) ;
15+
16+ //2. removes "P" from the string
17+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
18+ //3. Ensures the string is at least 3 characters long by adding 0 to the start
19+ const pounds = paddedPenceNumberString . substring (
20+ 0 ,
21+ paddedPenceNumberString . length - 2
22+ ) ;
23+
24+ //4. Extracts everything except the last 2 digits of paddedPenceNumberString
25+ const pence = paddedPenceNumberString
26+ . substring ( paddedPenceNumberString . length - 2 )
27+ . padEnd ( 2 , "0" ) ;
28+ return pence
29+ }
30+ //5. takes the last 2 digits as the pence from paddedPenceNumberStringCollapse comment
31+ console . log ( toPounds ( "399p" ) )
32+ console . log ( toPounds ( "400p" ) )
33+ console . log ( toPounds ( "301p" ) )
34+ console . log ( toPounds ( "302p" ) )
You can’t perform that action at this time.
0 commit comments