Skip to content

Commit 2f652a2

Browse files
committed
Complete Sprint 2 interpret exercises
1 parent 4a0c07f commit 2f652a2

3 files changed

Lines changed: 126 additions & 13 deletions

File tree

‎Sprint-2/3-mandatory-interpret/1-percentage-change.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ let carPrice = "10,000";
22
let priceAfterOneYear = "8,543";
33

44
carPrice = Number(carPrice.replaceAll(",", ""));
5-
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
66

77
const priceDifference = carPrice - priceAfterOneYear;
88
const percentageChange = (priceDifference / carPrice) * 100;
@@ -12,11 +12,31 @@ console.log(`The percentage change is ${percentageChange}`);
1212
// Read the code and then answer the questions below
1313

1414
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
// There are 5 function/method calls:
16+
// Line 4: carPrice.replaceAll(",", "")
17+
// Line 4: Number(...)
18+
// Line 5: priceAfterOneYear.replaceAll(",", "")
19+
// Line 5: Number(...)
20+
// Line 10: console.log(...)
1521

1622
// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?
23+
// The error originally comes from line 5.
24+
// priceAfterOneYear was declared with const, but line 5 tries to assign a new value to it.
25+
// A variable declared with const cannot be reassigned, so JavaScript throws a TypeError.
26+
// To fix the error, change const to let because priceAfterOneYear needs to be reassigned.
1727

1828
// c) Identify all the lines that are variable reassignment statements
29+
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
30+
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1931

2032
// d) Identify all the lines that are variable declarations
33+
// Line 1: let carPrice = "10,000";
34+
// Line 2: let priceAfterOneYear = "8,543";
35+
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
36+
// Line 8: const percentageChange = (priceDifference / carPrice) * 100;
2137

2238
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
39+
// carPrice.replaceAll(",", "") removes all commas from the string,
40+
// changing "10,000" to "10000".
41+
// Number(...) then converts the string "10000" into the number 10000.
42+
// This allows carPrice to be used correctly in mathematical calculations.
Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,71 @@
11
const movieLength = 8784; // length of movie in seconds
22

3+
// Test values tried:
4+
// const movieLength = 9893;
5+
// const movieLength = 223;
6+
// const movieLength = 50;
7+
// const movieLength = 345;
8+
// const movieLength = 600;
9+
// const movieLength = 400;
10+
// const movieLength = 90;
11+
// const movieLength = 189;
12+
13+
function movieFormatting(num) {
14+
if (num < 10) {
15+
return "0" + num.toString();
16+
} else {
17+
return num.toString();
18+
}
19+
}
20+
321
const remainingSeconds = movieLength % 60;
422
const totalMinutes = (movieLength - remainingSeconds) / 60;
523

624
const remainingMinutes = totalMinutes % 60;
725
const totalHours = (totalMinutes - remainingMinutes) / 60;
826

9-
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
27+
const result = `${movieFormatting(totalHours)}:${movieFormatting(remainingMinutes)}:${movieFormatting(remainingSeconds)}`;
1028
console.log(result);
1129

1230
// For the piece of code above, read the code and then answer the following questions
1331

1432
// a) How many variable declarations are there in this program?
33+
// There are 6 variable declarations in the original program:
34+
// movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result.
1535

1636
// b) How many function calls are there?
37+
// There is 1 function call in the original program:
38+
// console.log(result).
1739

1840
// c) Using documentation, explain what the expression movieLength % 60 represents
19-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
41+
// The % operator is the remainder operator. It returns the remainder after division.
42+
// Here, movieLength % 60 gives the number of seconds left over after dividing
43+
// the total movie length in seconds by 60.
44+
// For example, with movieLength = 8784, the remainder is 24,
45+
// so remainingSeconds is 24.
2046

2147
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
48+
// First, remainingSeconds is subtracted from movieLength.
49+
// This removes the leftover seconds and leaves a value that can be divided
50+
// evenly by 60. The result is then divided by 60 to convert the seconds
51+
// into the total number of whole minutes.
52+
// For movieLength = 8784, totalMinutes is 146.
2253

2354
// e) What do you think the variable result represents? Can you think of a better name for this variable?
55+
// result represents the movie duration in hours, minutes and seconds.
56+
// A more descriptive variable name could be movieDurationHHMMSS.
57+
// For movieLength = 8784, the formatted result is "02:26:24".
2458

2559
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
60+
// I tested the code with multiple values of movieLength, including:
61+
// 9893, 223, 50, 345, 600, 400, 90 and 189.
62+
63+
// The original code works for the positive whole-number values I tested,
64+
// but it does not always format the output in HH:MM:SS format.
65+
// If the hours, minutes, or seconds are less than 10, for example 5 or 7,
66+
// they are displayed as a single digit instead of two digits.
67+
68+
// I created the movieFormatting() function to add a leading 0 when a value
69+
// is less than 10. Otherwise, the function returns the value as a string.
70+
// I then tested the updated code with multiple movieLength values and
71+
// the tested values produced the expected HH:MM:SS formatted output.
Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,74 @@
11
const penceString = "399p";
2-
32
const penceStringWithoutTrailingP = penceString.substring(
43
0,
54
penceString.length - 1
65
);
7-
86
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
97
const pounds = paddedPenceNumberString.substring(
108
0,
119
paddedPenceNumberString.length - 2
1210
);
13-
1411
const pence = paddedPenceNumberString
1512
.substring(paddedPenceNumberString.length - 2)
1613
.padEnd(2, "0");
17-
1814
console.log(`£${pounds}.${pence}`);
15+
// This program takes a string representing a price in pence.
16+
// The program then builds up a string representing the price in pounds.
17+
18+
// Step-by-step breakdown:
19+
20+
// 1. const penceString = "399p";
21+
// Initialises the penceString variable with the string "399p".
22+
// This represents a price of 399 pence.
23+
24+
// 2. penceString.length - 1
25+
// penceString has a length of 4. Subtracting 1 gives 3.
26+
// This is used to identify the position before the final "p".
27+
28+
// 3. penceString.substring(0, penceString.length - 1)
29+
// substring() extracts the characters from index 0 up to, but not including,
30+
// index 3. This removes the trailing "p" and produces the string "399".
31+
32+
// 4. const penceStringWithoutTrailingP = ...
33+
// Stores the result "399", so the price now contains only the numeric characters.
34+
35+
// 5. penceStringWithoutTrailingP.padStart(3, "0")
36+
// padStart() makes sure the string contains at least 3 characters.
37+
// If it has fewer than 3 characters, "0" is added to the beginning.
38+
// For "399", no padding is needed, so the value remains "399".
39+
// This is useful for smaller values such as "99", which would become "099".
40+
41+
// 6. const paddedPenceNumberString = ...
42+
// Stores the padded string. For the current input, its value is "399".
43+
44+
// 7. paddedPenceNumberString.length - 2
45+
// This calculates the position that separates the pounds from the final
46+
// two digits representing pence. For "399", the length is 3, so 3 - 2 = 1.
47+
48+
// 8. paddedPenceNumberString.substring(
49+
// 0,
50+
// paddedPenceNumberString.length - 2
51+
// )
52+
// Extracts the characters before the final two digits.
53+
// For "399", this extracts "3", which represents the pounds.
54+
55+
// 9. const pounds = ...
56+
// Stores the pounds part of the price. In this example, pounds is "3".
57+
58+
// 10. paddedPenceNumberString
59+
// .substring(paddedPenceNumberString.length - 2)
60+
// substring() starts two characters from the end of the string.
61+
// For "399", it extracts "99", which represents the pence part.
1962

20-
// This program takes a string representing a price in pence
21-
// The program then builds up a string representing the price in pounds
63+
// 11. .padEnd(2, "0")
64+
// Makes sure the pence part contains at least two characters.
65+
// If necessary, "0" is added to the end until the string has a length of 2.
66+
// In this example, "99" already has two characters, so it remains "99".
2267

23-
// You need to do a step-by-step breakdown of each line in this program
24-
// Try and describe the purpose / rationale behind each step
68+
// 12. const pence = ...
69+
// Stores the final pence part. In this example, pence is "99".
2570

26-
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
71+
// 13. console.log(`£${pounds}.${pence}`);
72+
// Uses a template literal to combine the pound sign, pounds value,
73+
// decimal point and pence value.
74+
// With the input "399p", the final output is "£3.99".

0 commit comments

Comments
 (0)