-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-to-pounds.js
More file actions
72 lines (52 loc) · 2.85 KB
/
Copy path3-to-pounds.js
File metadata and controls
72 lines (52 loc) · 2.85 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
let penceString = "399p";
let penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);
let paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
let pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
let pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
console.log(`£${pounds}.${pence}`);
// 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 start with
// const penceString = "399p";
//
// Initialises a string variable with the value "399p".
// penceString variable now store the value: "399p"
// const penceStringWithoutTrailingP = penceString.substring(
// 0,
// penceString.length - 1
// );
// What it does: takes a substring of penceString from index 0 up to (but not including) penceString.length - 1.
// And removes the last character (the trailing "p") so we are left with just the numeric part.
// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// What it does: ensures the numeric string has at least 3 characters by adding leading "0" characters.
// The subsequent logic expects at least three digits so we can safely slice off "pounds" (all except the last two digits) and "pence" (last two digits). After that padding can handles small values like "5p" while maintaing correct indexing.
// Result stored: paddedPenceNumberString === "399"
// const pounds = paddedPenceNumberString.substring(
// 0,
// paddedPenceNumberString.length - 2
// );
// What it does: takes the substring from index 0 up to (but not including) the last two characters. Those leading characters represent whole pounds.
// Note: Because of padStart(3, "0"), this always returns at least one character (for values < 100 it returns "0" or more).
// const pence = paddedPenceNumberString
// .substring(paddedPenceNumberString.length - 2)
// .padEnd(2, "0");
// What it does (first part): .substring(length - 2) returns the final two characters (the pence digits).
// What it does (second part): .padEnd(2, "0") ensures the result has at least two characters by adding trailing zeros if needed.
// Example: "399".substring(1) → "99". .padEnd(2,"0") → still "99". So pence === "99".
// console.log(`£${pounds}.${pence}`);
// What it does: prints a formatted pounds-and-pence string and symbol to the console, using template interpolation.
// Why: final result showing what user would expect the representation of the price.
// Example output: £3.99
let numericPence = parseInt(penceString.replace(/\D/g, ""), 10);
pounds = Math.floor(numericPence / 100);
pence = String(numericPence % 100).padStart(2, "0");