Skip to content

Commit 9a33391

Browse files
committed
Answer questions in 3-to-pounds.js
1 parent 81af5ee commit 9a33391

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring(
66
);
77

88
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9+
910
const pounds = paddedPenceNumberString.substring(
1011
0,
1112
paddedPenceNumberString.length - 2
@@ -25,3 +26,47 @@ console.log(`£${pounds}.${pence}`);
2526

2627
// To begin, we can start with
2728
// 1. const penceString = "399p": initialises a string variable with the value "399p"
29+
30+
31+
// Answers
32+
33+
34+
// 2. const penceStringWithoutTrailingP = penceString.substring(
35+
// 0,
36+
// penceString.length - 1
37+
// );
38+
// This declares and initialises a new variable penceStringWithoutTrailingP that is a substring of
39+
// penceString, starting at position 0 and up to but not including the last position. So the new variable
40+
// gets the value "399", the p is left behind.
41+
42+
43+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
44+
// We now want our value to be at least 3 digits long, but in case it is shorter, we want to fill up with zeros at
45+
// the start so it becomes 3 digits long. padStart does that, the 3 specifies minimum amount of digits and the "0"
46+
// specifies what to fill with. The result gets saved in new variable paddedPenceNumberString.
47+
48+
49+
// 4. const pounds = paddedPenceNumberString.substring(
50+
// 0,
51+
// paddedPenceNumberString.length - 2
52+
// );
53+
// Now we save the pounds amount into new variable pounds. We get the amount (which could be any amount of digits long)
54+
// by creating a substring from paddedPenceNumberString starting at position 0 and ending before the next to
55+
// last digit. The last two digits are the pence amount and they aren't included in pounds.
56+
57+
58+
// 5. const pence = paddedPenceNumberString
59+
// .substring(paddedPenceNumberString.length - 2)
60+
// .padEnd(2, "0");
61+
// Similarly to the previous operation, here we save the pence amount into new variable pence. We get it by
62+
// using substring on paddedPenceNumberString again, this time starting two digits from the end, saving the
63+
// final two digits into pence. Then, if the digits are fewer than 2, we chain a padEnd to add "0" until pence
64+
// is 2 digits long.
65+
// After trying different values, it seems like it is unnecessary to have the padEnd, since the pence will
66+
// always already be 2 digits long. Even if penceString started with just 1 digit, "0"s will have been padded
67+
// earlier at the beginning of the string, so pence can always be 2 digits and the padEnd will never take effect.
68+
69+
70+
// 6. console.log(`£${pounds}.${pence}`);
71+
// Using a template literal, here we log the £ sign followed by the the pounds amount, a period and the pence
72+
// amount, which is formatted to understand easier than the original penceString was.

0 commit comments

Comments
 (0)