Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WM4 | FATIMA_SAFANA | Module-Structuring-and-Testing-Data | WEEK5 #220

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
solution to to-pounds
  • Loading branch information
Fatimasfn committed Dec 7, 2024
commit 43de2f54bea78fd8853ea3cea67c55eb7c7ea6b8
46 changes: 46 additions & 0 deletions Sprint-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
@@ -4,3 +4,49 @@
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs

function toPound(amountInPence){
// removes p
const penceStringWithoutTrailingP = amountInPence.substring(0, amountInPence.length - 1);
// makes sure the length is 3
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//returns the pounds from the string i.e the first number in the string
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
// returns a the pence, i.e the substring(length-2) then pads it with 0 to make sure length is 2
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");

return (`£${pounds}.${pence}`);
}

// test to check function penceToPound 1digit input
const currentOutput = toPound("30p");
const targetOutput = "£0.30";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

// test to check function penceToPound 3digit input
const currentOutput1 = toPound("454p");
const targetOutput1 = "£4.54";
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
);

// test to check function penceToPound 1digit input
const currentOutput2 = toPound("4p");
const targetOutput2 = "£0.04";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// test to check function penceToPound 0 edge case
const currentOutput3 = toPound("0p");
const targetOutput3 = "£0.00";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);