Skip to content

Commit fc5d38c

Browse files
committed
added breakdown of each line in this program
1 parent d87544b commit fc5d38c

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

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

Lines changed: 13 additions & 3 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
@@ -23,5 +23,15 @@ console.log(`£${pounds}.${pence}`);
2323
// You need to do a step-by-step breakdown of each line in this program
2424
// Try and describe the purpose / rationale behind each step
2525

26-
// To begin, we can start with
2726
// 1. const penceString = "399p": initialises a string variable with the value "399p"
27+
28+
// 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1); initialises a variable that takes the value of variable penceString and uses the method substring() to remove the "p" from "399p" so the variable penceStringWithoutTrailingP now stores the string "399"
29+
30+
//3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); initialises a variable called paddedPenceNumberString that takes the value of variable penceStringWithoutTrailingP and uses the method .padStart() to make sure that the string is always 3 characters long, if it's shorter javascript will pad the value out with a 0 at the start of the value. e.g 39 = 039 or 9 = 009. so paddedPenceNumberString stores "399"
31+
32+
//4.const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2,); initialises a variable that called pounds that takes the value inside paddedPenceNumberString and uses the method subString() on it to remove the last two numbers from the padded string value. So the variable pounds now stores the string "3" which represents £3
33+
34+
//5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
35+
//initialises a variable called pence that takes the value of the variable paddedPenceNumberString and runs the .substring() method on it and isolates the last two numbers returning 99. the method .padEnd() then looks at this value and if it's two characters long it wont do anything, if it's less it'll add a 0 to the end of the value. In this case it produced 99 so it doesn't do anything. So the variable pence now stores "99" which represents £.99
36+
37+
//6. console.log(`£${pounds}.${pence}`); This will combine and print the final price using a template literal so the console will print (£3.99)

0 commit comments

Comments
 (0)