Skip to content

Commit cb7593e

Browse files
committed
implement 3
1 parent c53ae02 commit cb7593e

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,31 @@
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 toPounds(str)
9+
{
10+
// 1. const penceString = "399p": initialises a string variable with the value "399p"
11+
const penceStringWithoutTrailingP = str.substring(
12+
0,
13+
str.length - 1
14+
);
15+
16+
//2. removes "P" from the string
17+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
18+
//3. Ensures the string is at least 3 characters long by adding 0 to the start
19+
const pounds = paddedPenceNumberString.substring(
20+
0,
21+
paddedPenceNumberString.length - 2
22+
);
23+
24+
//4. Extracts everything except the last 2 digits of paddedPenceNumberString
25+
const pence = paddedPenceNumberString
26+
.substring(paddedPenceNumberString.length - 2)
27+
.padEnd(2, "0");
28+
return pence
29+
}
30+
//5. takes the last 2 digits as the pence from paddedPenceNumberStringCollapse comment
31+
console.log(toPounds("399p"))
32+
console.log(toPounds("400p"))
33+
console.log(toPounds("301p"))
34+
console.log(toPounds("302p"))

0 commit comments

Comments
 (0)