Skip to content

Commit ddaeb22

Browse files
committed
answered all questions
1 parent 4e87ea5 commit ddaeb22

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,28 @@ 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+
// 3. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1):
30+
// Takes the characters from index 0 up to (but not including) the last character.
31+
// penceString.length is 4, so this is substring(0,3), resulting in "399".
32+
// The purpose is to remove the "p" so only digits remain.
33+
34+
// 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0")
35+
// Adds "0"s to the start until the string is at least 3 characters long.
36+
// The purpose is to guarantee at least one digit for pounds and two digits for pence.
37+
38+
// 9. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);
39+
// The purpose is to make the pounds all the digits before the last two, by taking everything except the last two characters: substring(0,1), giving "3"
40+
41+
// 14. const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
42+
// substring(1) takes from index 1 to the end, giving "99".
43+
// padEnd(2, "0") adds "0"s to the end until it is two characters long, so "99" stays "99".
44+
// The purpose is too ensure the pence are always the last two digits.
45+
// The padEnd does not change anything here since line 8 already guarantees at leas 3 characters, so the last two characters always exist.
46+
47+
// 18. console.log(`£${pounds}.${pence}`);
48+
// Call function using a template literal to combine "£", the pounds, a ".", and the pence, then prints "£3.99".
49+
50+
51+
52+

0 commit comments

Comments
 (0)