Skip to content

Commit 7290c7f

Browse files
committed
detailed explanation about the code
1 parent 4607d5a commit 7290c7f

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const penceString = "399p";
22

33
const penceStringWithoutTrailingP = penceString.substring(
44
0,
5-
penceString.length - 1
5+
penceString.length - 1,
66
);
77

88
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
99
const pounds = paddedPenceNumberString.substring(
1010
0,
11-
paddedPenceNumberString.length - 2
11+
paddedPenceNumberString.length - 2,
1212
);
1313

1414
const pence = paddedPenceNumberString
@@ -25,3 +25,43 @@ 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. From line 3 to line 6 const penceStringWithoutTrailingP is declared and it will hold the value of
30+
// penceString.substring(0, penceString.length - 1)
31+
// which is "399". We got this value by the method substring(0, penceString.length - 1) called by penceString
32+
// Breaking down the expression penceString.substring(0, penceString.length - 1)
33+
// a) penceString.length - 1 this counts the number of characters in the penceString String and then subtracts 1 from it (4-1=3)
34+
// then it will look like this penceString.substring(0, 3)
35+
// b) penceString.substring(0, penceString.length - 1) then the substring method called by penceString
36+
// is used produce a slice of small string from a big string
37+
// it takes two arguments the first one tells where to start slicing and the second one tells where to stop
38+
// (but the last character is not included)
39+
// Therefore const penceStringWithoutTrailingP will be assigned "399"
40+
41+
// 3. In line 8 const paddedPenceNumberString is declared and it will hold the value of penceStringWithoutTrailingP.padStart(3, "0")
42+
// which is still "399". In this expression we used the method padStart called by penceStringWithoutTrailingP and this method is basically
43+
// adds characters at the start of our String. It takes two arguments the first one is telling how many characters we want in our String
44+
// and second argument tells the character that needs to be added so that we get out desired number of characters in our String.
45+
// since our String length is 3 the method will not do anything
46+
47+
// 4. From line 9 to line 12 const pounds is declared and it will hold the value of paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,)
48+
// which is "3". We got this value by the method substring(0, paddedPenceNumberString.length - 2,) called by paddedPenceNumberString
49+
// Breaking down the expression paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,)
50+
// a) paddedPenceNumberString.length - 2 this counts the number of characters in the paddedPenceNumberString then subtracts 2 from it (3-2=1)
51+
// then it will look like this paddedPenceNumberString.substring(0, 1)
52+
// b) paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,) then the substring method called by paddedPenceNumberString is used to
53+
// produce a slice of small string from a big string
54+
// it takes two arguments the first one tells where to start slicing and the second one tells where to stop
55+
// (but the last character is not included)
56+
// Therefore const pounds will assigned "3"
57+
58+
// 5. From line 14 to line 16 const pence is declared and it will hold the value of
59+
// paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") which is "99"
60+
// Breaking down the expression
61+
// a) paddedPenceNumberString.length - 2 this will count the number of characters in paddedPenceNumberString and then subtracts 2 from it (3-2=1)
62+
// paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) then this substring method will produce small string from it starting from index 1 which will be "99"
63+
//then finally paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") the padEnd method will add characters at the end of the String
64+
// and will take two arguments first will tell how many characters we need in our string and the second will tell the character that needs to be added
65+
// since our String length is 2 the method will do nothing
66+
67+
// 6. Finally line 18 will print out the desired format we have added what to be printed inside the back-ticks if they are variables we will put them in ${}

0 commit comments

Comments
 (0)