Skip to content

Commit 783db28

Browse files
committed
JavaScript code interpretation
1 parent 70b961f commit 783db28

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,28 @@ 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 5 function calls in this file. They occur on the following 3 lines:
16+
// carPrice = Number(carPrice.replaceAll(",", ""));
17+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
18+
// console.log(`The percentage change is ${percentageChange}`);
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+
// The code contains one syntax error on the following line:
22+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
23+
// A comma is missing between the arguments inside replaceAll(). It should be:
24+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1725

1826
// c) Identify all the lines that are variable reassignment statements
27+
// The lines that are variable reassignment statements are as follows:
28+
// carPrice = Number(carPrice.replaceAll(",", ""));
29+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
1930

2031
// d) Identify all the lines that are variable declarations
32+
// The lines that contain variable declarations are as follows:
33+
// let carPrice = "10,000";
34+
// let priceAfterOneYear = "8,543";
35+
// const priceDifference = carPrice - priceAfterOneYear;
36+
// const percentageChange = (priceDifference / carPrice) * 100;
2137

2238
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
39+
// It removes the comma from the car price string and converts it into a number so it can be used in calculations.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
// Six variables are declared in this program.
1516

1617
// b) How many function calls are there?
18+
// In this program, the only function call is:
19+
// console.log(result);
1720

1821
// c) Using documentation, explain what the expression movieLength % 60 represents
1922
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
23+
// The expression movieLength % 60 yields the remaining seconds after converting the total seconds into full minutes.
2024

2125
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
26+
// It calculates the total number of full minutes in the movie length.
2227

2328
// e) What do you think the variable result represents? Can you think of a better name for this variable?
29+
// It represents the movie length formatted as a time string in hours:minutes:seconds format.
30+
// A better name for this variable could be const formattedTime.
2431

2532
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
33+
// The code does not produce the desired output for all possible values of movieLength.
34+
// It does not work correctly for decimal values.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ 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):
29+
// removes the trailing "p" from the string, leaving only the numeric part ("399")
30+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
31+
// ensures that the numeric string has at least three characters by adding leading zeros if necessary
32+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2):
33+
// extracts all the digits except the last two, representing the number of pounds
34+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"):
35+
// extracts the last two digits representing the pence
36+
// 6. console.log(`£${pounds}.${pence}`):
37+
// yields the final price in pounds and pence using the format £pounds.pence

0 commit comments

Comments
 (0)