Skip to content

Commit 095a3ea

Browse files
committed
Give a step-by-step breakdown of each line in this program
1 parent 03cb451 commit 095a3ea

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

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

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,168 @@ 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+
// Answer
30+
const penceString = "399p"
31+
// declares a variable penceString and assigns a value "399p" to it.
32+
33+
const penceStringWithoutTrailingP = penceString.substring(
34+
0,
35+
penceString.length - 1
36+
);
37+
// Step-by-step breakdown
38+
// 1. const
39+
40+
// What it means:
41+
// const is short for constant.
42+
// 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"
108+
109+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
110+
// Step-by-step breakdown
111+
// 1. const
112+
113+
// Keyword meaning:
114+
// const is short for constant.
115+
// It tells JavaScript that we’re creating a new variable whose value cannot be changed later.
116+
117+
// Why use it here?
118+
// We only need to store the processed result; we’re not planning to change it again.
119+
120+
// 2. paddedPenceNumberString
121+
122+
// This is the variable’s name.
123+
124+
// 3. =
125+
126+
// equals sign here is the assignment operator.
127+
128+
// It means “store the result on the right-hand side into the variable on the left-hand side.”
129+
130+
// 4. penceStringWithoutTrailingP
131+
132+
// This is another variable that was defined earlier in the program.
133+
134+
// It holds a string like "399", "50", or "5", after removing the “p” at the end of the original price (like "399p" → "399").
135+
136+
// 5. .padStart(3, "0")
137+
138+
// This is a string method — something you can call on a string to make it do something.
139+
140+
// Let’s break it down further:
141+
142+
// a) The dot (.)
143+
144+
// The dot connects the variable (penceStringWithoutTrailingP) with a method (a built-in action it can perform).
145+
146+
// In English: “Hey JavaScript, take this string and do the padStart operation on it.”
147+
148+
// b) padStart(...)
149+
150+
// This is the method name.
151+
152+
// It “pads” the start (the beginning) of a string with extra characters until it reaches a certain length.
153+
154+
// c) The parentheses (3, "0")
155+
156+
// These are the arguments — pieces of information you pass to the method.
157+
158+
// The first argument (3) means:
159+
// “Make the string at least 3 characters long.”
160+
161+
// The second argument ("0") means:
162+
// “If it’s shorter than that, add zeros ("0") at the start.”
163+
164+
// Putting it all together
165+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
166+
// 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+
const pounds = 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+
const pence = 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
190+
// so, print £3.99 to the terminal
191+
192+

0 commit comments

Comments
 (0)