@@ -2,14 +2,14 @@ const penceString = "399p";
22
33const penceStringWithoutTrailingP = penceString . substring (
44 0 ,
5- penceString . length - 1
5+ penceString . length - 1 ,
66) ;
77
88const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
99
1010const pounds = paddedPenceNumberString . substring (
1111 0 ,
12- paddedPenceNumberString . length - 2
12+ paddedPenceNumberString . length - 2 ,
1313) ;
1414
1515const pence = paddedPenceNumberString
@@ -27,25 +27,21 @@ console.log(`£${pounds}.${pence}`);
2727// To begin, we can start with
2828// 1. const penceString = "399p": initialises a string variable with the value "399p"
2929
30-
3130// Answers
3231
33-
3432// 2. const penceStringWithoutTrailingP = penceString.substring(
35- // 0,
36- // penceString.length - 1
33+ // 0,
34+ // penceString.length - 1
3735// );
3836// This declares and initialises a new variable penceStringWithoutTrailingP that is a substring of
3937// penceString, starting at position 0 and up to but not including the last position. So the new variable
4038// gets the value "399", the p is left behind.
4139
42-
4340// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
4441// We now want our value to be at least 3 digits long, but in case it is shorter, we want to fill up with zeros at
4542// the start so it becomes 3 digits long. padStart does that, the 3 specifies minimum amount of digits and the "0"
4643// specifies what to fill with. The result gets saved in new variable paddedPenceNumberString.
4744
48-
4945// 4. const pounds = paddedPenceNumberString.substring(
5046// 0,
5147// paddedPenceNumberString.length - 2
@@ -54,7 +50,6 @@ console.log(`£${pounds}.${pence}`);
5450// by creating a substring from paddedPenceNumberString starting at position 0 and ending before the next to
5551// last digit. The last two digits are the pence amount and they aren't included in pounds.
5652
57-
5853// 5. const pence = paddedPenceNumberString
5954// .substring(paddedPenceNumberString.length - 2)
6055// .padEnd(2, "0");
@@ -66,7 +61,6 @@ console.log(`£${pounds}.${pence}`);
6661// always already be 2 digits long. Even if penceString started with just 1 digit, "0"s will have been padded
6762// earlier at the beginning of the string, so pence can always be 2 digits and the padEnd will never take effect.
6863
69-
7064// 6. console.log(`£${pounds}.${pence}`);
7165// Using a template literal, here we log the £ sign followed by the the pounds amount, a period and the pence
72- // amount, which is formatted to understand easier than the original penceString was.
66+ // amount, which is formatted to understand easier than the original penceString was.
0 commit comments