File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Sprint-2/3-mandatory-implement Expand file tree Collapse file tree 1 file changed +29
-0
lines changed 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+ function toPounds ( penceString ) {
8+ // Remove the trailing "p" from the string
9+ const penceStringWithoutTrailingP = penceString . substring (
10+ 0 ,
11+ penceString . length - 1
12+ ) ;
13+
14+ // Ensure the number has at least 3 digits by padding from the left with zeros
15+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
16+
17+ // Extract the pound portion
18+ const pounds = paddedPenceNumberString . substring (
19+ 0 ,
20+ paddedPenceNumberString . length - 2
21+ ) ;
22+
23+ // Extract the pence portion and pad right if needed
24+ const pence = paddedPenceNumberString
25+ . substring ( paddedPenceNumberString . length - 2 )
26+ . padEnd ( 2 , "0" ) ;
27+
28+ // Return the formatted result
29+ return `£${ pounds } .${ pence } ` ;
30+ }
31+
32+ // Example usage:
33+ console . log ( toPounds ( "399p" ) ) ;
34+ console . log ( toPounds ( "155p" ) ) ;
35+ console . log ( toPounds ( "75p" ) ) ;
You can’t perform that action at this time.
0 commit comments