Skip to content

Commit 174fccd

Browse files
committed
string methods
1 parent 06c13e5 commit 174fccd

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ const penceStringWithoutTrailingP = penceString.substring(
44
0,
55
penceString.length - 1
66
);
7+
console.log(penceStringWithoutTrailingP)
78

89
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
10+
console.log(paddedPenceNumberString)
11+
912
const pounds = paddedPenceNumberString.substring(
1013
0,
1114
paddedPenceNumberString.length - 2
1215
);
16+
console.log(pounds)
1317

1418
const pence = paddedPenceNumberString
1519
.substring(paddedPenceNumberString.length - 2)
1620
.padEnd(2, "0");
17-
21+
console.log(pence)
1822
console.log(${pounds}.${pence}`);
1923

2024
// This program takes a string representing a price in pence
@@ -23,5 +27,27 @@ console.log(`£${pounds}.${pence}`);
2327
// You need to do a step-by-step breakdown of each line in this program
2428
// Try and describe the purpose / rationale behind each step
2529

30+
2631
// To begin, we can start with
2732
// 1. const penceString = "399p": initialises a string variable with the value "399p"
33+
34+
35+
// 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
36+
// Initialises a string variable with the value of a new string removing the last character from penceString.
37+
// "399"
38+
39+
40+
//3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
41+
// Initializes a new variable with the value of a new string the length of which is 3 characters.
42+
// "399"
43+
44+
45+
//4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);
46+
// Initializes a new variable containing a string made from paddedPenceNumberString, with the last two characters removed using the substring() method.
47+
// "3"
48+
49+
50+
//5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
51+
// Extracts the last two characters of paddedPenceNumberString and ensures the result is at least two characters long by adding zeros to the end if necessary.
52+
// Initializes a new variable containing a string made from paddedPenceNumberString, with the first characters removed using the substring() method.
53+
// padEnd() method ensures the result is at least two characters, if the string is less than 2 char padEnd adds zeros.

0 commit comments

Comments
 (0)