Skip to content

Commit af47636

Browse files
updated penceString file and expanded comments to include breakdown and rationale of the program
1 parent 976fb67 commit af47636

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

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

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const penceString = "399p";
1+
const penceString = "9p";
22

33
const penceStringWithoutTrailingP = penceString.substring(
44
0,
@@ -25,3 +25,98 @@ console.log(`£${pounds}.${pence}`);
2525

2626
// To begin, we can start with
2727
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28+
29+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1) : intialises a variable
30+
// with the value of the penceString variable with the last character "p" removed.
31+
32+
// a. This is achieved by using the substring method on the penceString variable
33+
// the substring method enables the program to manipulate the penceSting variable by targeting and removing characters in the string.
34+
35+
// b. The first argument of the substring method indicates the starting index; (0) which is 399p[0] = "3"
36+
37+
// c. The second argument of the substring method indicates the ending index (penceString.length - 1)
38+
// (.length) returns the length of the string object which is 4, and (-1) targets the last character in the string which is "p".
39+
//
40+
// d. Therefore, the substring method will return a new string that starts from index 0 and ends at index 3 which is "399"
41+
42+
// e. If const penceString = "1399p" the penceStringWithoutTrailingP variable will have the value "1399"
43+
// because the substring method will indicate the starting index as (0 which = "1")
44+
// and the ending index as (penceString.length - 1) which is 5 - 1 = 4, which is "p"
45+
// therefore it will return a new string that starts from index 0 and ends at index 4 which is "1399"
46+
47+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0") : initialises a variable
48+
// with the value of the penceStringWithoutTrailingP variable with padding added to the start of the string
49+
// until it reaches a total length of 3 characters.
50+
51+
// a. This is achieved by using the padStart method on the penceStringWithoutTrailingP variable
52+
// the padStart method enables the program to manipulate the penceStringWithoutTrailingP variable
53+
// by adding padding to the start of the string until it reaches a specified length.
54+
55+
// b. The first argument of the padStart method indicates the target length of the resulting string which is 3 in this case.
56+
57+
// c. The second argument of the padStart method indicates the string to use to fill or pad which is "0" in this case.
58+
//
59+
// d. Therefore, if the penceStringWithoutTrailingP variable has a length of less than 3 characters,
60+
// the padStart method will add "0" characters to the start of the string until it reaches a total length of 3 characters.
61+
// In this case, since penceStringWithoutTrailingP is "399" which already has a length of 3 characters,
62+
// the padStart method will not add any padding and paddedPenceNumberString will also be "399".
63+
64+
// e. if const penceStringWithoutTrailingP = "99" the paddedPenceNumberString variable will have the value "099"
65+
// because the padStart method will add one "0" character to the start of the string until it reaches a total length of 3 characters.
66+
67+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) : initialises a variable
68+
// with the value of the paddedPenceNumberString variable with the last two characters removed.
69+
70+
// a. This is achieved by using the substring method on the paddedPenceNumberString variable
71+
// the substring method enables the program to manipulate the paddedPenceNumberString variable by targeting and removing characters in the string.
72+
73+
// b. The first argument of the substring method indicates the starting index; (0) which is 399[0] = "3"
74+
75+
// c. The second argument of the substring method indicates the ending index (paddedPenceNumberString.length - 2)
76+
// (.length) returns the length of the string object which is 3, and (-2) targets the last two characters in the string which is "99"
77+
//
78+
// d. Therefore it will return a new string that starts from index 0 and ends at index 1 which is "3"
79+
80+
// d. If const paddedPenceNumberString = "099" the pounds variable will have the value "0" because the substring method
81+
// will indicate the starting index as (0 which = "0") and the ending index as (paddedPenceNumberString.length - 2)
82+
// which is 3 - 2 = 1, which is "9" therefore it will return a new string that starts from index 0 and ends at index 0 which is "0"
83+
84+
// e. if penceString = "1399p" ; const penceStringWithoutTrailingP = "1399" ; const paddedPenceNumberString = "1399"
85+
// because the padStart method will not add any padding since the length of penceStringWithoutTrailingP is already 4 characters
86+
// which is greater than the target length of 3 characters. therefore the const pounds variable will have the value "13"
87+
// because the substring method will indicate the starting index as (0 which = "1") and the ending index as
88+
// (paddedPenceNumberString.length - 2) which is 4 - 2 = 2, which is "9" therefore it will return a new string that starts from index 0
89+
// and ends at index 1 which is "13"
90+
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.
94+
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.
97+
98+
// b. The first argument of the substring method indicates the starting index which is (paddedPenceNumberString.length - 2)
99+
// 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.
106+
107+
// e. The second argument of the padEnd method indicates the string to use to fill or pad which is "0" in this case.
108+
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"
115+
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"
120+
121+
// 6. console.log(`£${pounds}.${pence}`) : outputs the final result to the console in the format of "£pounds.pence"
122+
// where pounds and pence are the values of the pounds and pence variables respectively.

0 commit comments

Comments
 (0)