Skip to content

Commit 499906a

Browse files
committed
Update 3-to-pounds.js
Explain each step one by one.
1 parent 22aca43 commit 499906a

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ 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+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable that contains the original string without the last character (the "p").
29+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): creates a new string variable that pads the previous string with leading zeros until it is at least 3 characters long. eg. "45" becomes "045" and "7" becomes "007".
30+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): creates a new string variable that contains the first part of the padded string, which represents the pounds. It takes all characters except the last two. eg. "045" becomes "0" and "399" becomes "3".
31+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): creates a new string variable that contains the last two characters of the padded string, which represents the pence. It also pads it with trailing zeros if necessary to ensure it is 2 characters long. eg. "045" becomes "45" and "7" becomes "70".
32+
// 6. console.log(`£${pounds}.${pence}`): outputs the final price in pounds and pence format to the console. For example, if penceString is "399p", it will output "£3.99". If penceString is "45p", it will output "£0.45". If penceString is "7p", it will output "£0.07".

0 commit comments

Comments
 (0)