Skip to content

Commit 7e158fa

Browse files
All exercises in 3-mandatory-interpret completed
1 parent b9828e4 commit 7e158fa

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
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,12 @@ 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-
15+
// Answer: There are 5 function calls on lines 4, 5 and 10
1616
// 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?
17-
17+
// Answer: line 5 there is a comma missing in the replaceAll parameters.
1818
// c) Identify all the lines that are variable reassignment statements
19-
19+
// Answer: Lines 4 and 5
2020
// d) Identify all the lines that are variable declarations
21-
21+
// Answer: Lines 1, 2, 7 and 8
2222
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
23+
// Answer: It removes all the commas from the string carPrice and turns it into a number that can be used in calculations
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const movieLength = 8784; // length of movie in seconds
1+
const movieLength = "839f"; // length of movie in seconds
22

33
const remainingSeconds = movieLength % 60;
44
const totalMinutes = (movieLength - remainingSeconds) / 60;
@@ -12,14 +12,15 @@ 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-
15+
// Answer: There is 6 variable declarations
1616
// b) How many function calls are there?
17-
17+
// Answer: There is one function call on line 10
1818
// c) Using documentation, explain what the expression movieLength % 60 represents
1919
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
20-
20+
// Answer: this finds the remainder of movieLength when its divided by 60
2121
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
22-
23-
// e) What do you think the variable result represents? Can you think of a better name for this variable?
24-
22+
// Answer: The expressions uses the modulo function to work out the remainder value which in this case works out the remaining seconds of the total movie length
2523
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
24+
// Answer: No it only works as intended when a Non negative integer is used however when Decimal, negative, or string values it does not work as intended.
25+
// Decimal values will display decimal seconds, which does not cause an error, but the output may not be in the expected time format.
26+
// Negative values will produce negative time values, and string values may produce NaN if they cannot be converted into a number.

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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–6. const penceStringWithoutTrailingP takes the initial constant variable with the value of "399p" and uses the substring function to make a new string
30+
// without the "p". It uses the .length property to return the number of characters in the string and subtracts 1 so that substring stops before the last
31+
// character, which will always remove the "p".
32+
33+
// 8. the next variable paddedPenceNumberString takes the previous string which only has number characters and checks if it is made up of three characters if it is not it
34+
// is padded with "0" at the start of the string till there are three characters the reason for this is the minimum amount of characters needed to represent pounds and
35+
// pence can be represented by is 3.
36+
37+
//// 9-12. The next variable pounds takes the previous variable paddedPenceNumberString and uses the substring function to create a new string without the last
38+
// two characters. It uses .length - 2 to stop before the final two characters because those characters represent the pence leaving only the characters
39+
// that represent the pounds.
40+
41+
// 14-16. The next variable pence takes paddedPenceNumberString and uses the substring function starting from two characters before the end of the string.
42+
// This creates a new string containing only the final two characters which represent the pence. The padEnd function then adds "0" to the end of the
43+
// string until it contains two characters making sure that the pence is always displayed using two digits.
44+
45+
// 18. The console.log uses a template literal to combine the pound sign with the pounds and pence variables. A decimal point is placed between the two
46+
// variables so that the final value is displayed as a price in pounds, for example "£3.99".

0 commit comments

Comments
 (0)