Skip to content

Commit 91836ab

Browse files
Complete time format and to pounds exercises
1 parent 258aecc commit 91836ab

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

Sprint-1/3-mandatory-interpret/2-time-format.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,40 @@ console.log(result);
2424

2525
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
2626

27+
/*
2728
29+
a)variables declarations
30+
31+
1. There are 6 variable declarations
32+
33+
b) Function calls
34+
35+
There is only 1 function call
36+
37+
console.log(result);
38+
39+
c)Using documentation, explain what the expression movieLength % 60 represents
40+
41+
According to the JavaScript documentation, the remainder (%) operator returns the remainder left over after one number is divided by another.
42+
43+
So movieLength % 60 means Divide movieLength by 60 and return the remainder.
44+
45+
d) Interpret line 4. What does the expression assigned to totalMinutes mean?
46+
47+
It means remove the leftover seconds from the total movie length, then divide by 60 to calculate the total number of whole minutes.
48+
49+
50+
e) What do you think the variable result represents? Can you think of a better name?
51+
52+
It stores the movie length formatted as hours:minutes:seconds
53+
54+
A better variable name could be formattedTime
55+
56+
57+
f) Try experimenting with different values of movieLength.
58+
59+
It works correctly for positive whole numbers (integers) representing seconds. However, there are some cases where it doesn't produce ideal results like:
60+
61+
const movieLength = -100;
62+
63+
*/

Sprint-1/3-mandatory-interpret/3-to-pounds.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const penceString = "399p";
1+
const penceString = "380p";
22

33
const penceStringWithoutTrailingP = penceString.substring(
44
0,
@@ -25,3 +25,54 @@ console.log(`£${pounds}.${pence}`);
2525

2626
// To begin, we can start with
2727
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28+
29+
/*
30+
31+
Step 1:
32+
we create a variable called penceString and store the text "339p".
33+
34+
Step 2:
35+
36+
a) . (dot) lets us use something that belongs to the string.
37+
b) length is a property that tells us how many characters are in the string. There are 4 characters so returns 4.
38+
c) penceString.length - 1 which means 4 - 1 = 3 We want to remove the last character ("p"), so we stop before it.
39+
d) penceString.substring(0, 3) substring(start, end) start = where to begin end = where to stop (not including that position) So: substring(0, 3)
40+
41+
Start at character 0 and stop before character 3. Why? We don't want the "p" anymore because we only need the number.
42+
43+
Step 3:
44+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
45+
46+
It means If this string is shorter than length, add the character to the beginning until it reaches that length. Our value already has 3 characters so nothing changes.
47+
48+
Step 4:
49+
50+
const pounds = paddedPenceNumberString.substring(
51+
0,
52+
paddedPenceNumberString.length - 2);
53+
54+
a) paddedPenceNumberString.length returns 3 then 3 - 2 is 1.
55+
56+
Why? because the last 2 digits are always the pence and everything before them is the pounds.
57+
58+
Step 5:
59+
60+
const pence = paddedPenceNumberString
61+
.substring(paddedPenceNumberString.length - 2)
62+
.padEnd(2, "0");
63+
64+
a) substring(length - 2) the length is 3 so substring(1) means Start at index 1 and keep going to the end.
65+
66+
b) .padEnd(2, "0") which means if the string is shorter than 2 characters, add "0" to the end.
67+
68+
69+
Step 6:
70+
71+
console.log(`£${pounds}.${pence}`);
72+
73+
Inside it is a template literal, which uses backticks (`): and `£${pounds}.${pence}` means insert the value of this variable here.
74+
75+
If pounds = "3" pence = "99" then javascript prints 3.99
76+
77+
78+
*/

0 commit comments

Comments
 (0)