Skip to content

Commit d2baabe

Browse files
committed
3-mandatory-interpret are done.
1 parent 647ad66 commit d2baabe

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

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

Lines changed: 8 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,18 @@ 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 the fourth line Number () and replaceAll ().
16+
// In the fifth line same as line 4 Number () and replaceAll ().
17+
// Last one in seventh line console.log ().
1518

1619
// 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+
// The error is in line 5. The error is occurring because there is a missing comma in the replaceAll function. The correct code should be: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1721

1822
// c) Identify all the lines that are variable reassignment statements
23+
// Line 4 and line 5 are variable reassignment statements. In line 4, carPrice is being reassigned with the new value after removing the comma and converting it to a number. In line 5, priceAfterOneYear is being reassigned in the same way.
1924

2025
// d) Identify all the lines that are variable declarations
26+
// Line 1 and line 2 are variable declarations. In line 1, carPrice is declared and initialized with the string value "10,000". In line 2, priceAfterOneYear is declared and initialized with the string value "8,543".
2127

2228
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
29+
// The expression removes the comma from the price string and converts it into a number. This allows JavaScript to use the value in calculations.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ 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: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result.
1516

1617
// b) How many function calls are there?
18+
// There is 1 function call in this program: console.log(result).
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 % operator is the remainder operator.
23+
// It returns the remainder left over after one number is divided by another number.
24+
// MovieLength % 60 finds the remaining seconds after dividing the total movie length by 60.
25+
// This is used to calculate how many seconds are left after converting the total seconds into minutes.
2026

2127
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
22-
28+
// Take away the leftover seconds, then convert the rest into minutes.
2329
// e) What do you think the variable result represents? Can you think of a better name for this variable?
24-
30+
// The movie length displayed as hours:minutes:seconds.
31+
// A better name for this variable could be movieDuration.
2532
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
33+
// Yes, it works for all movie lengths except for negative and text values.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,18 @@ 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+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1):
30+
// removes the last character ("p") from the string, leaving only the number part "399".
31+
32+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
33+
// makes sure the pence value has at least 3 characters by adding zeros at the start if needed.
34+
35+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2):
36+
// takes the first part of the string to get the pounds value.
37+
38+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"):
39+
// takes the last two characters to get the pence value and adds a zero at the end if needed.
40+
41+
// 6. console.log(`£${pounds}.${pence}`):
42+
// prints the final price in pounds and pence format.

0 commit comments

Comments
 (0)