File tree Expand file tree Collapse file tree 1 file changed +20
-5
lines changed
Sprint-2/3-mandatory-implement Expand file tree Collapse file tree 1 file changed +20
-5
lines changed Original file line number Diff line number Diff line change 1- // Define a function that converts kilograms to pounds
2- function toPounds ( kg ) {
3- const pounds = kg * 2.20462 ;
4- return Number ( pounds . toFixed ( 2 ) ) ; // return as number, not string
1+ function toPounds ( penceString ) {
2+ const penceStringWithoutTrailingP = penceString . substring ( 0 , penceString . length - 1 ) ;
3+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
4+
5+ const pounds = paddedPenceNumberString . substring (
6+ 0 ,
7+ paddedPenceNumberString . length - 2
8+ ) ;
9+
10+ const pence = paddedPenceNumberString
11+ . substring ( paddedPenceNumberString . length - 2 )
12+ . padEnd ( 2 , "0" ) ;
13+
14+ return `£${ pounds } .${ pence } ` ;
515}
616
7- module . exports = toPounds ;
17+ // Test calls
18+ console . log ( toPounds ( "399p" ) ) ; // Output: £3.99
19+ console . log ( toPounds ( "5p" ) ) ; // Output: £0.05
20+ console . log ( toPounds ( "45p" ) ) ; // Output: £0.45
21+ console . log ( toPounds ( "999p" ) ) ; // Output: £9.99
22+ console . log ( toPounds ( "2p" ) ) ; // Output: £0.02
You can’t perform that action at this time.
0 commit comments