Skip to content

Commit 5476287

Browse files
committed
Mnadatory - interpret completed
1 parent 49747e6 commit 5476287

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

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

Lines changed: 16 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;
@@ -13,10 +13,25 @@ console.log(`The percentage change is ${percentageChange}`);
1313

1414
// a) How many function calls are there in this file? Write down all the lines where a function call is made
1515

16+
// There are 5 function calls in this file.
17+
// Line 4 : Number() and replaceAll()
18+
// Line 5 : Number() and replaceAll()
19+
// Line 10 : console.log()
20+
1621
// 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?
1722

23+
// The error is on line 5, where there is a syntax error in the replaceAll method call. The second argument is missing quotes.
24+
// To fix it, we need to add quotes around the empty string: replaceAll(",", "").
25+
1826
// c) Identify all the lines that are variable reassignment statements
1927

28+
// Line 4 and Line 5
29+
2030
// d) Identify all the lines that are variable declarations
2131

32+
// Line 1, 2, 7, 8.
33+
2234
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
35+
36+
// The expression is first calling the replaceAll method on the carPrice string to remove all commas. "10,000" ==> "10000".
37+
// Then it converts the resulting string into a number using the Number() function. "10000" ==> 10000.
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const movieLength = 8784; // length of movie in seconds
22

33
const remainingSeconds = movieLength % 60;
4-
const totalMinutes = (movieLength - remainingSeconds) / 60;
4+
const totalMinutes = (movieLength - remainingSeconds) / 60; //146
55

6-
const remainingMinutes = totalMinutes % 60;
7-
const totalHours = (totalMinutes - remainingMinutes) / 60;
6+
const remainingMinutes = totalMinutes % 60; //26
7+
const totalHours = (totalMinutes - remainingMinutes) / 60; // 0
88

99
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
1010
console.log(result);
@@ -13,13 +13,28 @@ console.log(result);
1313

1414
// a) How many variable declarations are there in this program?
1515

16+
// There are 6 Variable declarations.
17+
1618
// b) How many function calls are there?
1719

20+
// There are 2 function calls. Console.log() and the template literal used.
21+
1822
// c) Using documentation, explain what the expression movieLength % 60 represents
1923
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2024

25+
// movieLength % 60 gives the leftover seconds after converting seconds into whole minutes.
26+
2127
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
2228

29+
//It gets the total minutes by removing the leftover seconds and converting the remaining seconds into minutes by dividing by 60.
30+
2331
// e) What do you think the variable result represents? Can you think of a better name for this variable?
2432

33+
//remainingMinutes is the minutes part in an hours:minutes:seconds display
34+
// It can be named as minutesDisplay.
35+
2536
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
37+
38+
// The code will work for all positive integer values of MovieLength.
39+
// If movieLength is negative, the code will still run but it will give a negative time which doesn't make sense in this context.
40+
// If movieLength is not an integer, it will still run but it may give unexpected results due to the way the modulus operator works with non-integer values.
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
const penceString = "399p";
22

3+
// This creates a string variable that represents a price in pence, with the letter p at the end.
4+
35
const penceStringWithoutTrailingP = penceString.substring(
46
0,
57
penceString.length - 1
68
);
79

10+
// This takes the string from the start up to (but not including) the last character, so it removes the trailing "p" and leaves "399"
11+
812
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
13+
14+
// This makes sure the pence number has at least 3 characters by adding zeros to the front if it’s too short (so "5" would become "005"), and "399" stays "399".
915
const pounds = paddedPenceNumberString.substring(
1016
0,
1117
paddedPenceNumberString.length - 2
1218
);
1319

20+
// This takes everything except the last 2 digits, which gives the pounds part (for "399" it becomes "3").
21+
1422
const pence = paddedPenceNumberString
1523
.substring(paddedPenceNumberString.length - 2)
1624
.padEnd(2, "0");
1725

18-
console.log(${pounds}.${pence}`);
26+
//This takes the last 2 digits to get the pence part (for "399" it becomes "99") and ensures it is always 2 digits long by padding with zeros on the right if needed.
1927

20-
// This program takes a string representing a price in pence
21-
// The program then builds up a string representing the price in pounds
28+
console.log(${pounds}.${pence}`);
2229

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
30+
// This prints the final formatted price by inserting pounds and pence into a template string, producing £3.99.
2531

26-
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
32+
// This program takes a string representing a price in pence

0 commit comments

Comments
 (0)