Skip to content

Commit bb852dd

Browse files
committed
Mandatory interprets are done
1 parent f71288b commit bb852dd

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

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

Lines changed: 7 additions & 5 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,13 @@ 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-
15+
// line: 4 and line 5 and it is replaceAll().
1616
// 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?
17-
17+
// error is coming from line 5. The error is because of not putting comma between the inputs of replaceAll function.
18+
// To fix the problem just add a comma between the inputs of replaceAll function.
1819
// c) Identify all the lines that are variable reassignment statements
19-
20+
// Line 4 and 5.
2021
// d) Identify all the lines that are variable declarations
21-
22+
// we have variable declarations at lines 1, 2, 7 and 8.
2223
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
24+
// the purpose of that expression is to turn into number the string and replace all commas with an empty space.
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const movieLength = 8784; // length of movie in seconds
1+
const movieLength = 8724; // length of movie in seconds
22

33
const remainingSeconds = movieLength % 60;
44
const totalMinutes = (movieLength - remainingSeconds) / 60;
@@ -12,14 +12,17 @@ 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-
15+
// we have 6 variable declarations.
1616
// b) How many function calls are there?
17-
17+
// here we have only one which is console.log().
1818
// c) Using documentation, explain what the expression movieLength % 60 represents
1919
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
20-
20+
// Remainder operator (%), returns the leftover when one operand divided by the other operand.
21+
// movieLength % 60 expression represents the remaining seconds.
2122
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
22-
23+
// that expression turns the seconds into minutes.
2324
// e) What do you think the variable result represents? Can you think of a better name for this variable?
24-
25+
// the result variable represents the movie length in Hour, minutes and seconds.
26+
// a better name can be "movieLength".
2527
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
28+
// as I changed the value of the movieLength the results has changed too.It means this code works with any value.
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
const penceString = "399p";
22

3-
const penceStringWithoutTrailingP = penceString.substring(
4-
0,
5-
penceString.length - 1
6-
);
3+
const penceStringWithoutTrailingP =
4+
penceString.substring(0, penceString.length - 1); // => "399";
75

8-
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9-
const pounds = paddedPenceNumberString.substring(
10-
0,
11-
paddedPenceNumberString.length - 2
12-
);
6+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // => "399";
7+
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); // => "3"
138

149
const pence = paddedPenceNumberString
15-
.substring(paddedPenceNumberString.length - 2)
16-
.padEnd(2, "0");
10+
.substring(paddedPenceNumberString.length - 2) // => "99"
11+
.padEnd(2, "0");
1712

18-
console.log(${pounds}.${pence}`);
13+
console.log(${pounds}.${pence}`); // => "3.99"
1914

2015
// This program takes a string representing a price in pence
2116
// The program then builds up a string representing the price in pounds
@@ -24,4 +19,12 @@ console.log(`£${pounds}.${pence}`);
2419
// Try and describe the purpose / rationale behind each step
2520

2621
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
22+
// 1. const penceString = "399p": initializes a string variable with the value "399p"
23+
// 2. const penceStringWithoutTrailingP uses the substring()method to remove the last character.
24+
// 3. const paddedPenceNumberString tries to add "0" at the beginning, but the length is already 3; so it does nothing.
25+
// Its worthy to talk about padStart() method. we use this to add a character at the beginning of a string.
26+
// 4. const pound uses the substring() method on paddedPenceNumberString variable and remove its last two characters.Its value ll be "3".
27+
// 5. const pence uses substring() on paddedPenceNumberString variable and take its length and then subtract it with 2 ...
28+
// which will remain like this .substring(1), so it take the index 1 till the last which is only one more index that its character is 9 ...
29+
// so the final value ll be "99". then uses padEnd( 2, "0") that can not add "0" at the end, as we set the length "2".
30+
// 6. Lastly, we print the value of our pound and pence variable. which is "3.99".

0 commit comments

Comments
 (0)