Skip to content

Commit 02ebac8

Browse files
Simplify pence extraction logic
Removed unnecessary padding for pence extraction.
1 parent 078e937 commit 02ebac8

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

Sprint-1/3-mandatory-interpret/3-to-pounds.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const pounds = paddedPenceNumberString.substring(
1212
);
1313

1414
const pence = paddedPenceNumberString
15-
.substring(paddedPenceNumberString.length - 2)
16-
.padEnd(2, "0");
15+
.substring(paddedPenceNumberString.length - 2);
1716

1817
console.log(${pounds}.${pence}`);
1918

@@ -88,35 +87,22 @@ console.log(`£${pounds}.${pence}`);
8887
// (paddedPenceNumberString.length - 2) which is 4 - 2 = 2, which is "9" therefore it will return a new string that starts from index 0
8988
// and ends at index 1 which is "13"
9089

91-
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") : initialises a variable
92-
// with the value of the last two characters of the paddedPenceNumberString variable with padding added to the end of the string
93-
// until it reaches a total length of 2 characters.
90+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) : initialises a variable
91+
// with the value of the last two characters of the paddedPenceNumberString variable
9492

95-
// a. This is achieved by using the substring method on the paddedPenceNumberString variable to extract the last two characters of the string,
96-
// and then using the padEnd method to add padding to the end of the string until it reaches a specified length.
93+
// a. This is achieved by using the substring method on the paddedPenceNumberString variable to extract the last two characters of the string.
9794

9895
// b. The first argument of the substring method indicates the starting index which is (paddedPenceNumberString.length - 2)
9996
// which targets the last two characters in the string. The ending index is not provided, so it will extract until the end of the string.
100-
//
101-
102-
// c. The padEnd method is then used on the resulting string from the substring method to add padding to the end of the string
103-
// until it reaches a total length of 2 characters.
104-
105-
// d. The first argument of the padEnd method indicates the target length of the resulting string which is 2 in this case.
10697

107-
// e. The second argument of the padEnd method indicates the string to use to fill or pad which is "0" in this case.
98+
// c. Because paddedPenceNumberString was padded to a minimum length of 3 in step 3, this operation is guaranteed to
99+
// return exactly two characters, making further padding unnecessary.
108100

109-
// f. Therefore, if the paddedPenceNumberString variable has a length of less than 2 characters, the padEnd method will add "0" characters
110-
// to the end of the string until it reaches a total length of 2 characters.
111-
// In this case, since paddedPenceNumberString is "399" which has a length of 3 characters,
112-
// the substring method will extract the last two characters "99"
113-
// and then the padEnd method will not add any padding since the length of the resulting string is already 2 characters.
114-
// therefore pence will be "99"
101+
// d. For example, if paddedPenceNumberString is "399", the substring method will extract the last two characters "99".
102+
// therefore pence will be "99".
115103

116-
// g. if penceString = "9p" ; const penceStringWithoutTrailingP = "9" ; const paddedPenceNumberString = "009" ;
117-
// const pounds = "0" ; const pence = "09" because the substring method will extract the last two characters "09"
118-
// and then the padEnd method will not add any padding since the length of the resulting string is already 2 characters.
119-
// therefore pence will be "09"
104+
// e. if penceString = "9p" ; const penceStringWithoutTrailingP = "9" ; const paddedPenceNumberString = "009" ;
105+
// const pounds = "0" ; const pence = "09" because the substring method will extract the last two characters "09".
120106

121107
// 6. console.log(`£${pounds}.${pence}`) : outputs the final result to the console in the format of "£pounds.pence"
122108
// where pounds and pence are the values of the pounds and pence variables respectively.

0 commit comments

Comments
 (0)