Skip to content

Commit f399c78

Browse files
committed
finished toPounds exercise
1 parent a718a88 commit f399c78

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,27 @@
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(pence) {
9+
//Declare penceString variable
10+
let penceString = pence;
11+
// Store penceString value with pence on the end and make sure its at least 3 characters long add 0 at start if needed.
12+
let penceStringWithoutTrailingP = penceString
13+
.substring(0, penceString.length - 1)
14+
.padStart(3, "0");
15+
// copy everything apart from the last two characters
16+
let poundsResult = penceStringWithoutTrailingP.substring(
17+
0,
18+
penceStringWithoutTrailingP.length - 2,
19+
);
20+
// start at the last two characters and take everything from there to end and make sure that value is two characters long add 0 if needed
21+
let penceResult = penceStringWithoutTrailingP
22+
.substring(penceStringWithoutTrailingP.length - 2)
23+
.padEnd(2, "0");
24+
// return result of poundResult and penceResult in a template string
25+
return `£${poundsResult}.${penceResult}`;
26+
}
27+
//call function to see if works with different inputs
28+
console.log(toPounds("399p"));
29+
console.log(toPounds("1000p"));
30+
console.log(toPounds("15000p"));

0 commit comments

Comments
 (0)