Skip to content

Commit 0870089

Browse files
committed
docs: documentation on how works the call in each functions and using methods like substring and padStart to add and take part or the string
1 parent 188692d commit 0870089

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Sprint-1/interpret/percentage-change.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ console.log(`The percentage change is ${percentageChange}`);
3737

3838
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
3939

40-
// This expresion will work in the parenthesis first, replacing all commas in the string carPrice
40+
// This expression will work in the parenthesis first, replacing all commas in the string carPrice
4141
// with an empty string, so basically returning the string 10000 instead of "10,000".
4242
// Then the Number() function will convert that string "10000" into a number 10000.
4343
// secondly, js can not treat strings with commas as numbers, so we need to remove the commas

Sprint-1/interpret/to-pounds.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1+
//declare a constant variable named penceString and assign it the string value "399p"
12
const penceString = "399p";
23

34
const penceStringWithoutTrailingP = penceString.substring(
45
0,
56
penceString.length - 1
67
);
78

8-
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9-
const pounds = paddedPenceNumberString.substring(
10-
0,
11-
paddedPenceNumberString.length - 2
12-
);
9+
// here we remove the trailing 'p' from the string using the method substring() -1 of the total length
10+
console.log(penceStringWithoutTrailingP);
11+
12+
13+
//this line pads will contain a length of at least 3 characters, adding leading zeros if necessary
14+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");//399 or diff value -> 001
15+
16+
// here we extract the pounds part of the string by taking all characters except the last two eg. 399 -> 3
17+
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); //3
18+
19+
console.log(` this is the padStart adding 0 if its necessary ${paddedPenceNumberString}, and this here we just take the integer part ${pounds}`);
20+
1321

22+
// here we extract the pence part of the string by taking the last two characters eg. 399 -> 99
1423
const pence = paddedPenceNumberString
1524
.substring(paddedPenceNumberString.length - 2)
1625
.padEnd(2, "0");
1726

27+
console.log(` this is the pence part we take the last two digits ${pence}`);
1828
console.log(${pounds}.${pence}`);
1929

2030
// This program takes a string representing a price in pence

0 commit comments

Comments
 (0)