Skip to content

Commit 803168d

Browse files
function to convert pence to pounds and pence
1 parent 8878027 commit 803168d

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed
Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
// In Sprint-1, there is a program written in interpret/to-pounds.js
2-
3-
// You will need to take this code and turn it into a reusable block of code.
4-
// You will need to declare a function called toPounds with an appropriately named parameter.
5-
6-
// You should call this function a number of times to check it works for different inputs
7-
//const penceString = "399p";
8-
9-
const penceStringWithoutTrailingP = penceString.substring(
10-
0,
11-
penceString.length - 1
12-
);
1+
function getPenceString(penceString){
132

3+
const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
4+
//removes p
145
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
15-
const pounds = paddedPenceNumberString.substring(
16-
0,
17-
paddedPenceNumberString.length - 2
18-
);
19-
20-
const pence = paddedPenceNumberString
21-
.substring(paddedPenceNumberString.length - 2)
22-
.padEnd(2, "0");
6+
//puts zeros in front of number if less than 3 digits
7+
const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);
8+
//removes last 2 digits to get pounds
9+
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
10+
//gets last 2 digits to get pence, if less than 2 digits adds zeros to end of string but shouldnt be less than 3
11+
//due to padStart above
12+
return ${pounds}.${pence}`;
13+
// returns string with pounds and pence in correct format
14+
}
2315

24-
console.log(${pounds}.${pence}`);
16+
console.log(getPenceString("399p"));
17+
// tested with 123p 1200p 24589p 89p 9p 0p

0 commit comments

Comments
 (0)