File tree Expand file tree Collapse file tree 2 files changed +16
-6
lines changed
Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ console.log(`The percentage change is ${percentageChange}`);
3737
3838// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
3939
40- // This expresion will work in the parenthesis first, replacing all commas in the string carPrice
40+ // This expression will work in the parenthesis first, replacing all commas in the string carPrice
4141 // with an empty string, so basically returning the string 10000 instead of "10,000".
4242 // Then the Number() function will convert that string "10000" into a number 10000.
4343 // secondly, js can not treat strings with commas as numbers, so we need to remove the commas
Original file line number Diff line number Diff line change 1+ //declare a constant variable named penceString and assign it the string value "399p"
12const penceString = "399p" ;
23
34const penceStringWithoutTrailingP = penceString . substring (
45 0 ,
56 penceString . length - 1
67) ;
78
8- const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
9- const pounds = paddedPenceNumberString . substring (
10- 0 ,
11- paddedPenceNumberString . length - 2
12- ) ;
9+ // here we remove the trailing 'p' from the string using the method substring() -1 of the total length
10+ console . log ( penceStringWithoutTrailingP ) ;
11+
12+
13+ //this line pads will contain a length of at least 3 characters, adding leading zeros if necessary
14+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ; //399 or diff value -> 001
15+
16+ // here we extract the pounds part of the string by taking all characters except the last two eg. 399 -> 3
17+ const pounds = paddedPenceNumberString . substring ( 0 , paddedPenceNumberString . length - 2 ) ; //3
18+
19+ console . log ( ` this is the padStart adding 0 if its necessary ${ paddedPenceNumberString } , and this here we just take the integer part ${ pounds } ` ) ;
20+
1321
22+ // here we extract the pence part of the string by taking the last two characters eg. 399 -> 99
1423const pence = paddedPenceNumberString
1524 . substring ( paddedPenceNumberString . length - 2 )
1625 . padEnd ( 2 , "0" ) ;
1726
27+ console . log ( ` this is the pence part we take the last two digits ${ pence } ` ) ;
1828console . log ( `£${ pounds } .${ pence } ` ) ;
1929
2030// This program takes a string representing a price in pence
You can’t perform that action at this time.
0 commit comments