You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Try and describe the purpose / rationale behind each step
25
25
26
26
// To begin, we can start with
27
-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
27
+
/*
28
+
1. const penceString = "399p": initializes a string variable with the value "399p".
29
+
30
+
2. const penceStringWithoutTrailingP: this variable is now store a new value of penceString "399".
31
+
By using the .substring method(.slice method predecessor)it remove the p from the penceString value and
32
+
reassign the new value to variable penceStringWithoutTrailingP. It start from index position 0 in this case "3" and stop at the index 2 "9".
33
+
34
+
3. const paddedPenceNumberString: this variable main role is to check if the string value total length
35
+
at least 3, if the length short then padStart will add 0 the left from the first index(0) in this case "3" until the string length total is 3. In this case
36
+
it select the string number "3".
37
+
38
+
4. const pounds: this variable is role is to extract the pound value from the paddedPenceNumberString.
39
+
Inside the the paddedPenceNumberString subString method parameter it have 2 value 0 and paddedPenceNumberString total string length minus 1,
40
+
the 0 is the index of the string number that we would like to start while .length - 2 is to tell the SubString method where to stop in this case the first "9".
41
+
42
+
5) const pence: as the name suggest this variable main role is to extract he pence value from the paddedPenceNumberString variable.
43
+
How ever this variable there are 2 method is used.
44
+
-subString method is used to slice the string value "99" off the "399" value, how ever is now only have 1 value
45
+
is the length of the "399" is 3 now - 2 equal to 1 and no end value. The main reason is to select where the slice will begin stop at the end
46
+
of the string.
47
+
-padEnd method work exactly the same as padStart but in reverse. The padEnd method is checking to see is the paddedPenceNumberString string length at least total of 2,
48
+
if not they will add 0 from the selected index position from the string value "99" from the index 0 in this case is "9" to the right.
49
+
50
+
6)console.log(`£${pounds}.${pence}`): is to log and print out the value inside the terminal by using the string template, the back tick is to
51
+
define a template literal. Then inside the template the pound sign(£) is show the pound currency, the money sign before the curly bracket is to allow
52
+
embed variables in this case pounds and pence that separate by dot that print out a float string value.
0 commit comments