-
-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy path3-to-pounds.js
More file actions
40 lines (29 loc) · 1.59 KB
/
3-to-pounds.js
File metadata and controls
40 lines (29 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const penceString = "392p";
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
); //39772
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); //39772
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
); // 397
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0"); // 72
console.log(`£${pounds}.${pence}`); // 397.72
// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds
// You need to do a step-by-step breakdown of each line in this program
// Try and describe the purpose / rationale behind each step
// To begin, we can start with
// 1. const penceString = "399p": initializes a string variable with the value "399p"
// 2 to 6.const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
// removes the (p) at the end determined by removing the sudstring at the total length of the string -1
// 8.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// makes sure the string is at least 3 characters long and if it is not it padds it with "0"
// 9.const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2);
// gets the ammout of pounds by making a sub string that starts at the firts character (0) to the paddedPenceNumberString length -2 to exclued the last 2 characters (the p)
// 10.
// 18.console.log(`£${pounds}.${pence}`);
// gives the console the result using a string literal