Skip to content

Commit 7d7305a

Browse files
committed
completed 3-to-pounds.js with comments explanation all 1-6
1 parent dc05100 commit 7d7305a

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,34 @@ console.log(`£${pounds}.${pence}`);
2424
// Try and describe the purpose / rationale behind each step
2525

2626
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
27+
// 1. const penceString = "399p": initialises a string variable with the value "399p"
2828

2929
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
30-
// This takes the variable penceString from the start of the string index(0) to 1 before the end
31-
// and stops ie index (-1). and drops the last indexed character "p".
32-
33-
// 3.
30+
// This takes the variable penceString and uses the .substring() method to extract part of the string.
31+
// It starts at index 0 and ends at penceString.length - 1, which removes the last character and
32+
// outputs all characters except the trailing "p".
33+
// This returns "399".
34+
35+
// 3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
36+
// This takes the value of the penceStringWithoutTrailingP and uses .padStart() method to always return a minimum
37+
// length of 3 characters. if the value computed is not a least 3 characters, it outputs leading 0's to make
38+
// to make the string to 3 characters. If the string is 4 or more characters it just outputs the value without the leading 0's.
39+
// This returns "399". (examples: 45 returns 045, 1295 returns 1295)
40+
41+
// 4.const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2);
42+
// This takes the value of paddedPenceNumberString and uses the .substring() method to extract the first art of the string.
43+
// It starts at index 0 and ends at paddedPenceNumberString.length - 2, which removes the last two characters.
44+
// This leaves the pounds portion of the value.
45+
// This returns "3". (examples: 45 returns 0, 1295 returns 12)
46+
47+
// 5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
48+
// This takes the variable paddedPenceNumberString and extracts the last two characters using .substring(),
49+
// which represent the pence. If the string is shorter than 2 characters, .padEnd(2, "0") adds trailing zeros
50+
// to ensure the pence portion is always 2 digits.
51+
// This returns "99". (examples: 45 returns 45, 1295 returns 95)
52+
53+
// 6. console.log(`£${pounds}.${pence}`);
54+
// console.log() outputs the formatted money value to the console. It uses a template literal (backticks)
55+
// to embed the variables `pounds` and `pence` directly inside the string using ${} syntax.
56+
// The £ symbol is included at the start, and the dot separates pounds and pence.
57+
// This returns "£3.99" (examples: 45 returns £0.45, 1295 returns £12.95)

0 commit comments

Comments
 (0)