Skip to content

Commit 9a86ff0

Browse files
author
AlanGit-debug2604
committed
3-mandatory-implement 3-to-pounds.js Implement toPound function to convert whole pence string to formatted pounds
1 parent cf2bf33 commit 9a86ff0

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎Sprint-3/3-mandatory-implement/3-to-pounds.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,32 @@
44
// You will need to declare a function called toPounds with an appropriately named parameter.
55

66
// You should call this function a number of times to check it works for different inputs
7+
8+
//Function name toPound
9+
//identifier of prev code :
10+
// 1 penceString - user input ; Type: String, count length by .length
11+
// 2 penceStringWithoutTrailingP ; cut the tail p by .substring ()
12+
// 3 paddedPenceNumberString ; assign and prescribe target length of pence in three characters length by .padStart()
13+
// 4 pounds ; assign pounds from first character of paddedPenceNumberString, cut the last two character by .substring
14+
// 5 pence ; assign pence starting from last two character of paddedPenceNumberString
15+
// Parameter to name penceStringWithP
16+
17+
function toPound(wholePenceStringWithP) {
18+
const penceStringWithoutTrailingP = wholePenceStringWithP.substring(
19+
0,
20+
wholePenceStringWithP.length - 1,
21+
);
22+
23+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
24+
const pounds = paddedPenceNumberString.substring(
25+
0,
26+
paddedPenceNumberString.length - 2,
27+
);
28+
29+
const pence = paddedPenceNumberString.substring(
30+
paddedPenceNumberString.length - 2,
31+
);
32+
33+
return `£${pounds}.${pence}`;
34+
}
35+
console.log(toPound("399p"));

0 commit comments

Comments
 (0)