Skip to content

Commit 90c1eca

Browse files
committed
completed the 3-mandatory-interpret of sprint-1
1 parent 1ccf331 commit 90c1eca

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

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

Lines changed: 14 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,24 @@ 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 4 function calls in this file. The lines where a function call is made are:
16+
// Line 1: carPrice.replaceAll(",", "")
17+
// Line 2: priceAfterOneYear.replaceAll(",", "")
18+
// Line 3: Number(carPrice.replaceAll(",", ""))
19+
// Line 4: Number(priceAfterOneYear.replaceAll(",", ""))
1520

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?
22+
// The error is occurring on 4 where we are trying to convert the string values of carPrice and priceAfterOneYear to numbers using the Number function. The error is occurring because the replaceAll method is being called on a string that has not been updated with the new value after the first replaceAll call. To fix this problem, we can update the carPrice and priceAfterOneYear variables with the new values after the replaceAll calls. For example:
1723

1824
// c) Identify all the lines that are variable reassignment statements
25+
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
26+
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1927

2028
// d) Identify all the lines that are variable declarations
29+
// Line 1: let carPrice = "10,000";
30+
// Line 2: let priceAfterOneYear = "8,543";
31+
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
32+
// Line 8: const percentageChange = (priceDifference / carPrice) * 100;
2133

2234
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
35+
// The expression Number(carPrice.replaceAll(",","")) is first calling the replaceAll method on the carPrice string to remove all commas from the string. This is necessary because the presence of commas in a number string can cause issues when trying to convert it to a number. After the commas are removed, the resulting string is passed to the Number function, which converts the string into a numeric value. The purpose of this expression is to convert the carPrice string, which may contain commas, into a number that can be used for calculations.

Sprint-1/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+
// There are 6 variable declarations in this program.
1516

1617
// b) How many function calls are there?
18+
// There is 1 function call in this program, which is console.log(result).
1719

1820
// c) Using documentation, explain what the expression movieLength % 60 represents
21+
// The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. In this context, it is used to determine the number of seconds that are left after accounting for the full minutes in the movie length. Since there are 60 seconds in a minute, using the modulus operator (%) with 60 gives us the remaining seconds that do not make up a full minute. For example, if movieLength is 8784 seconds, then 8784 % 60 would give us the number of seconds remaining after dividing 8784 by 60, which is 24 seconds.
1922
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2023

2124
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
25+
// The expression assigned to totalMinutes is calculating the total number of minutes in the movie length. It does this by first subtracting the remaining seconds (calculated in the previous line) from the total movie length in seconds, which gives us the total number of seconds that are part of complete minutes. Then, it divides that result by 60 to convert the total seconds into minutes. For example, if movieLength is 8784 seconds and remainingSeconds is 24 seconds, then (8784 - 24) / 60 would give us the total number of minutes in the movie, which is 146 minutes.
2226

2327
// e) What do you think the variable result represents? Can you think of a better name for this variable?
24-
28+
// The variable result represents the formatted string that shows the length of the movie in hours, minutes, and seconds. A better name for this variable could be formattedMovieLength or movieDurationFormatted, as it more clearly indicates that it is a formatted representation of the movie length.
2529
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
30+
// This code will work for all non-negative integer values of movieLength, as it is designed to convert a length in seconds into a format of hours, minutes, and seconds. However, if movieLength is a negative value, the calculations may not make sense in the context of a movie length, and the output may not be meaningful.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ 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+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable that contains the original string without the last character (the "p"), resulting in "399"
29+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the string with leading zeros to ensure it has at least 3 characters, resulting in "399"
30+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the substring representing the pounds by taking all characters except the last two, resulting in "3"
31+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the substring representing the pence by taking the last two characters and pads it with trailing zeros if necessary, resulting in "99"
32+
// 6. console.log(`£${pounds}.${pence}`): outputs the final formatted price in pounds and pence, resulting in "£3.99"

0 commit comments

Comments
 (0)