Skip to content

Commit 7da57e5

Browse files
authored
Enhance clarity and documentation in 3-to-pounds.js
Refactor code to improve clarity and add comments explaining each step.
1 parent 24f5004 commit 7da57e5

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const penceString = "399p";
22

33
const penceStringWithoutTrailingP = penceString.substring(
44
0,
5-
penceString.length - 1
5+
penceString.length - 1,
66
);
77

88
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
99
const pounds = paddedPenceNumberString.substring(
1010
0,
11-
paddedPenceNumberString.length - 2
11+
paddedPenceNumberString.length - 2,
1212
);
1313

1414
const pence = paddedPenceNumberString
@@ -25,33 +25,37 @@ 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(
29-
// 0,
30-
// penceString.length - 1
31-
//);
32-
//INITIALISES A STRING VARIABLE AND ASSIGN ITS VALUE USEING THE penceString.substring
33-
// so what happins is penceString.substring is using the value from the application or calculation of (
34-
//0,
35-
// penceString.length - 1
28+
29+
//for this code I struggled with some part so I had to ask copilot to explain some lines of the code to me
30+
//const penceStringWithoutTrailingP = penceString.substring(
31+
// 0,
32+
// penceString.length - 1
3633
//);
37-
// we are tillING IT TO start from the first inex (0) and not to include index 3 which penceString.length than use the operator - to subtract 1
38-
// we intialise a string variable const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); and assign its value to be
34+
//const penceStringWithoutTrailingP: a string variable is initialised and assigned
35+
// using the = operator to be equal to the value of string penceString when the method substring is used to cut part of the string
36+
//so substring(0, penceString.length - 1);: means use substring method starting from index zero and end before the last character pencestring.length-1 : which is the length of the string
37+
//but how dom we know which index? Its is basically the characters in string where "399p" means we have 4 characters and we want to take 1
38+
//do 4 - 1 = 3 , so we are removing p character from the string so it becomes 399
39+
//this line says take the string penceStringWithoutTrailingP and use padStart()string method
40+
// (3, "0"); this means make the string 3 characters at least and if not add o to the beginning
3941
//const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
42+
//this line means take the string paddedPenceNumberString and use the
43+
//substring(0, length - 2) method to remove the last 2 characters , than store
44+
//the value into the pounds variable
4045
//const pounds = paddedPenceNumberString.substring(
41-
// 0,
42-
//paddedPenceNumberString.length - 2
46+
// 0,
47+
// paddedPenceNumberString.length - 2
4348
//);
44-
// we assign the value to be starting with MINUMUM LENGTH IS 3 CHARACTERS than if not ADD THE VALUE 0 TO THE BEGINNING
45-
// we initialise a variable const pounds and assign its value to be the value from using paddedPenceNumberString.substring
46-
//paddedPenceNumberString.substring value is found by starting from the first index WITH LENGTH 3 and ending with paddedPenceNumberString.length subtracted by the operator 2
4749

48-
// IN THIS LINE const pence = paddedPenceNumberString
49-
const pence = paddedPenceNumberString
50-
.substring(paddedPenceNumberString.length - 2)
51-
.padEnd(2, "0");
52-
53-
console.log(`£${pounds}.${pence}`);
54-
// we initialise a string variable and assign its value to be the paddedPenceNumberString variable with the value from using the substring method
55-
// the substring method is calculated by using the length property or the value of paddedPenceNumberString subtracting it by 2 using operator -
56-
// than using the method .padEnd to specify that if the length is not two add the value 0
57-
// last we ask the console to print the values in pounds and pence
50+
//this (paddedPenceNumberString.length - 2) mean find the position of the last 2 characters
51+
//in the string paddedPenceNumberString
52+
//substring()means take the last 2 characters you found from the string
53+
//.padEnd(2, "0"); means if the result is less than two characters , than add zero at
54+
//the end until it reaches two digits
55+
//const pence = paddedPenceNumberString
56+
//.substring(paddedPenceNumberString.length - 2)
57+
// .padEnd(2, "0");
58+
//this line uses console.log to print the value of ponds and pence
59+
//it uses literal template to find the value of pounds, uses a decimal point , than finds the value of pence
60+
//it also uses a pound sign at the front
61+
// console.log(`£${pounds}.${pence}`);

0 commit comments

Comments
 (0)