Skip to content

Commit 18a4902

Browse files
Complete 3-mandatory-interpret exercises
1 parent ba171ac commit 18a4902

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

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

Lines changed: 12 additions & 3 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,20 @@ 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+
// Answer: There are 5 function/method calls across 3 lines:
16+
// - Line 4: replaceAll(",", "") and Number(...)
17+
// - Line 5: replaceAll(",", "") and Number(...)
18+
// - Line 10: console.log(...)
1519

16-
// 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+
// 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?
21+
// Answer: On line 5, there is a typo in replaceAll(",", "") where the comma is outside the quotes or contains empty space before the closing quote (""")).
22+
// Fix line 5 by changing it to: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1723

1824
// c) Identify all the lines that are variable reassignment statements
25+
// Answer: Lines 4 and 5 (carPrice = ... and priceAfterOneYear = ...)
1926

2027
// d) Identify all the lines that are variable declarations
28+
// Answer: Lines 1, 2, 7, and 8 (let carPrice, let priceAfterOneYear, const priceDifference, const percentageChange)
2129

22-
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
30+
// e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing – what is the purpose of this expression?
31+
// Answer: .replaceAll(",", "") removes the comma from the price string ("10,000" becomes "10000"), and Number(...) converts that clean string into a numeric value so mathematical calculations can be performed on it

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ 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+
// Answer: 6 variable declarations (movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result).
1516

1617
// b) How many function calls are there?
18+
// Answer: 1 function call (console.log(result)).
1719

1820
// c) Using documentation, explain what the expression movieLength % 60 represents
19-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
21+
// Answer: % is the remainder (modulo) operator. It calculates the leftover seconds when movieLength is divided into full minutes (60-second chunks).
2022

2123
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
24+
// Answer: It subtracts the leftover seconds from the total movie length to get a exact multiple of 60, then divides by 60 to convert that duration into whole minutes.
2225

2326
// e) What do you think the variable result represents? Can you think of a better name for this variable?
27+
// Answer: It represents the movie length formatted as a HH:MM:SS time string. A clearer name would be formattedTime or formattedMovieLength.
2428

2529
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
30+
// Answer: It works for all non-negative numbers representing total seconds. However, if movieLength is negative or not a number (e.g., a string or null), it will output invalid time values or NaN.

‎Sprint-2/3-mandatory-interpret/3-to-pounds.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@ console.log(`£${pounds}.${pence}`);
2323
// You need to do a step-by-step breakdown of each line in this program
2424
// Try and describe the purpose / rationale behind each step
2525

26-
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
26+
// 1. const penceString = "399p": Initialises a string variable with the value "399p" representing the price in pence including the trailing letter 'p'.
27+
28+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): Extracts the numeric portion of the pence string by taking characters from index 0 up to (but not including) the last character, removing the 'p' ("399").
29+
30+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): Ensures the pence string is at least 3 digits long by adding leading zeros if necessary (e.g., "5" becomes "005"), ensuring there are enough digits to extract both pounds and pence.
31+
32+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): Extracts the pounds portion of the price by taking all digits except the last two ("3").
33+
34+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): Extracts the last two digits representing pence ("99") and ensures it is padded to 2 digits.
35+
36+
// 6. console.log(`£${pounds}.${pence}`): Formats and prints the final price string with the pound symbol, pounds amount, period, and pence amount ("£3.99").

0 commit comments

Comments
 (0)