Skip to content

Commit 134e9e9

Browse files
committed
mandatory interpret done
1 parent a2f23db commit 134e9e9

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ 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+
// Total: 5
16+
// 2 in line 4, 2 in line 5
17+
// 1 in line 10
1518

1619
// 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?
20+
// Error in line 5, missing comma after first arguement in replaceAll.
1721

1822
// c) Identify all the lines that are variable reassignment statements
23+
// Lines 4, and 5
1924

2025
// d) Identify all the lines that are variable declarations
26+
// Lines 1, 2, 7, 8
2127

2228
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
29+
// replaceAll function returns a new string with all commas removed, so that it is a valid input for Number.
30+
// Number takes an input and converts it to a number.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const movieLength = 8784; // length of movie in seconds
1+
const movieLength = -5784; // length of movie in seconds
22

33
const remainingSeconds = movieLength % 60;
44
const totalMinutes = (movieLength - remainingSeconds) / 60;
@@ -12,14 +12,23 @@ console.log(result);
1212
// For the piece of code above, read the code and then answer the following questions
1313

1414
// a) How many variable declarations are there in this program?
15+
// 6
1516

1617
// b) How many function calls are there?
18+
// 1
1719

1820
// c) Using documentation, explain what the expression movieLength % 60 represents
1921
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
22+
// % is the remainder operator in JavaScript. The expression represents the remainder after dividing
23+
// movieLength by 60, in this case: 24.
2024

2125
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
26+
// The total length of the movie in minutes, after taking away the fractional part (the 24 seconds).
2227

2328
// e) What do you think the variable result represents? Can you think of a better name for this variable?
29+
// The exact runtime of the movie, converted from seconds into hours, minutes and seconds format.
2430

2531
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
32+
// It works with all positive values of movieLength. For negative values, it produces negative hours, minutes, and seconds.
33+
// This is due to the fact that % is a remainder operator, not a true modulo operator. It always takes the sign of the divided.
34+
// So if you divide a negative number, the remainder will be negative.

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

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

33
const penceStringWithoutTrailingP = penceString.substring(
44
0,
@@ -25,3 +25,22 @@ 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+
// 3. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
30+
// removes the trailing p so that there's a clean number format to work with.
31+
32+
// 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
33+
// pad the start so that there's always a character (0) in the pound's place, and the 10th and 100th place of the
34+
// fractional part (pence). E.g. for 9 pence, the pre-padded number would be "9", by padding it with 0, you get
35+
// 009, which can easily be formatted as £0.09.
36+
37+
// 9. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
38+
// final two characters will always represent pence, be it "09" for 9 pence, or "90" for 90 pence
39+
// so the pound part will be anything preceding those two characters.
40+
41+
// 14. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
42+
// I guess, this is trying to ensure that pence always amounts to at least 2 digits, but it is unnecessary
43+
// as you're always taking the last two digits and padStart in line 8 ensures that the string always has at least 3 characters.
44+
45+
// 18. console.log(`£${pounds}.${pence}`);
46+
// this is just adding a pound symbol at the start and joining the pound and pence parts captured earlier with a dot.

0 commit comments

Comments
 (0)