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
// It’s a keyword in JavaScript used to declare a variable — a named place in memory where we store a value.
43
+
44
+
// What makes it special:
45
+
// Once you assign a value to a const variable, you cannot reassign it later.
46
+
47
+
// 2. penceStringWithoutTrailingP
48
+
49
+
// What it is:
50
+
// This is the name of the variable we’re creating.
51
+
52
+
53
+
// 3. =
54
+
55
+
// What it means:
56
+
// The equals sign here means assignment, not comparison.
57
+
// It tells JavaScript: “Store the value on the right-hand side into the variable on the left-hand side.”
58
+
59
+
// So, penceStringWithoutTrailingP will hold whatever value comes from the code on the right side.
60
+
61
+
// 4. penceString
62
+
63
+
// What it is:
64
+
// This is a variable that was created earlier in the code:
65
+
66
+
// 4. .substring(...)
67
+
68
+
// What it is:
69
+
// .substring() is a built-in method (a function that belongs to strings) in JavaScript.
70
+
71
+
// What it does:
72
+
// It extracts a section (or slice) of a string, starting and ending at specific positions (index numbers).
73
+
74
+
// The first number is where to start (inclusive).
75
+
76
+
// The second number is where to stop (exclusive, it doesn’t include that index).
77
+
78
+
// ( 0, penceString.length - 1 )
79
+
80
+
// These are the arguments (inputs) given to the substring() function.
81
+
82
+
// Let’s understand each one:
83
+
84
+
// 0
85
+
86
+
// This means: “Start from the very first character of the string.”
87
+
// In JavaScript, string positions start counting at 0, not 1.
88
+
89
+
// penceString.length
90
+
91
+
// .length is a property (not a function).
92
+
// It gives the number of characters in the string.
93
+
94
+
// For "399p", penceString.length is 4.
95
+
96
+
// penceString.length - 1
97
+
98
+
// This means “one less than the total length.”
99
+
100
+
// 4 - 1 = 3.
101
+
102
+
// So we’re telling JavaScript: “Stop right before the last character.”
103
+
104
+
// Putting it all together:
105
+
// penceString.substring(0, penceString.length - 1) means “Take the text in penceString, start at position 0, and stop right before the last character.”
106
+
// So for "399p", it returns "399" — it removes the final p.
107
+
// The variable penceStringWithoutTrailingP will now contain: "399"
// When JavaScript checks the length, it sees that it’s already 3 characters long, so it doesn’t add any zeros.
167
+
// the result "399" is stored in the variable "paddedPenceNumberString"
168
+
169
+
constpounds=paddedPenceNumberString.substring(
170
+
0,
171
+
paddedPenceNumberString.length-2
172
+
);
173
+
// paddedPenceNumberString.length means the length of the character "399" is 3.
174
+
// paddedPenceNumberString.length - 2 means 3 - 2 = 1
175
+
// paddedPenceNumberString.substring(0 , 1) means take everything from position 0 up till position 1 exclusive. This will return 3
176
+
// const pounds means store the value 3 inside the variable pounds.
177
+
178
+
constpence=paddedPenceNumberString
179
+
.substring(paddedPenceNumberString.length-2)
180
+
.padEnd(2,"0");
181
+
// paddedPenceNumberString.length means the length of the character "399" is 3.
182
+
// paddedPenceNumberString.length - 2 means 3 - 2 = 1
183
+
// .substring(paddedPenceNumberString.length - 2) means .substring(1) which means take everything from positon 1 to the end. This returns 99
184
+
// .padEnd(2, "0") means make sure the string has at least two characters. If not, add zeros ("0") at the end. for this exercise, 99..padEnd(2, "0") will return 99
185
+
// so, const pence means variable pence will store the value 99
186
+
187
+
console.log(`£${pounds}.${pence}`);
188
+
// console meaans terminal and log means print or show this message.
189
+
// `£${pounds}.${pence}` is a literal template which means £3.99
0 commit comments