Skip to content

Commit 19a2fea

Browse files
committed
completed all mandatory interpret of sprint 1
1 parent e1e5c79 commit 19a2fea

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 9 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,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+
// 5 function calls
16+
// replaceAll is called twice on line 4 and line 5
17+
// Number is called twice on line 4 and line 5
18+
// console.log is called once on line 7
1519

1620
// 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+
// its coming from line 4 and line 5 because we are trying to call the replaceAll method on a string that contains a comma, which is not a valid number. To fix this problem, we can remove the commas from the strings before converting them to numbers. We can do this by using the replaceAll method to replace all commas with an empty string before calling the Number function. This is already done in the code, so there should be no error when running it.
1722

1823
// c) Identify all the lines that are variable reassignment statements
24+
// line 4 and 5
1925

2026
// d) Identify all the lines that are variable declarations
27+
// line 1, 2, 7, 8.
2128

2229
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
30+
// The expression Number(carPrice.replaceAll(",","")) is first calling the replaceAll method on the carPrice string to remove all commas, and then it is converting the resulting string to a number using the Number function. The purpose of this expression is to convert the carPrice string, which may contain commas, into a numeric value that can be used for calculations.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ 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 variable declarations: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result
1516

1617
// b) How many function calls are there?
18+
// 1 function call: console.log
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+
// The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. This is used to determine how many seconds are left after accounting for the full minutes in the movie length. For example, if movieLength is 8784 seconds, then 8784 % 60 would give us the number of seconds that do not make up a full minute, which is 24 seconds in this case.
2023

2124
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
25+
// The expression (movieLength - remainingSeconds) / 60 calculates the total number of minutes in the movie length by subtracting the remaining seconds from the total seconds and then dividing by 60.
2226

2327
// e) What do you think the variable result represents? Can you think of a better name for this variable?
28+
// The variable result represents the formatted string of the movie length in hours, minutes, and seconds. A better name for this variable could be formattedMovieLength or movieLengthFormatted to make it more descriptive of its purpose.
2429

2530
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
31+
// This code will work for all non-negative integer values of movieLength. However, if movieLength is negative, the calculations for remainingSeconds, totalMinutes, remainingMinutes, and totalHours may not produce meaningful results.

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 last two characters to represent the pence and pads it with trailing zeros if necessary, resulting in "99"
32+
// 6. console.log(`£${pounds}.${pence}`): outputs the formatted price in pounds and pence, resulting in "£3.99"

0 commit comments

Comments
 (0)