Skip to content

Commit 2baa16a

Browse files
committed
Add detailed explanation for pence calculation in 3-to-pounds.js
1 parent 6468f29 commit 2baa16a

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

‎Sprint-2/3-mandatory-interpret/3-to-pounds.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@ console.log(`£${pounds}.${pence}`);
3535
// penceStringWithoutTrailingP is now '399'
3636
// padStart(3, "0") is a method used above which guarantees that the numeric string is at least 3 characters long, and if it is shorter add '0' to the front until it is 3 characters long.
3737
// padding to 3 ensures there is always at least one pounds digit and two pence digits.
38+
//
3839

3940
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
4041
// since paddedPenceNumberString is '399' and it's '.length' is 3
4142
// this line is saying: '399.substring(0, 3 - 2)', which becomes '399.substring(0, 1)'
4243
// 'substring' is a method used to grab characters from a string, starting from the left, from position '0' up to, but not including, position '1'
43-
// 'const pounds' would be assigned the new value: '3'
44+
// 'const pounds' would be assigned the new value: '3'
45+
46+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
47+
// since paddedPenceNumberString.length is '3'
48+
// then 'paddedPenceNumberString.substring(3 - 2)' becomes 'paddedPenceNumberString.substring(1)': this means grab everything from position '1' of the string to the end.
49+
// That is -> '399.substring(1)' becomes '99'
50+
// '.padEnd(2, "0") is another method that ensures the string is at least 2 characters long and adding '0' to the end if it's too short.
51+
// 'const pence' would be assigned the new value: '99'

0 commit comments

Comments
 (0)