Skip to content

Commit 878dae9

Browse files
first formula related file in javascript
1 parent 7b9e573 commit 878dae9

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

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

Lines changed: 22 additions & 9 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,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-
16-
// 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-
18-
// c) Identify all the lines that are variable reassignment statements
19-
20-
// d) Identify all the lines that are variable declarations
21-
22-
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
15+
/* there has five function in line 4 and and console . log also has one
16+
line 4 and 5 has two Number() function
17+
line 4 and 5 has two replaceALL() function
18+
line 10 has one console . log ()
19+
20+
/* 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+
In line five error shown in replaceAll("," "")); missing comma before last two comma(",","").
22+
23+
/* c) Identify all the lines that are variable reassignment statements
24+
line 4 carPrice = Number(carPrice.replaceAll(",", ""));
25+
line 5 priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
26+
As per the carPrice and priceAfterOneYear has been declared in line 1 and 2 by using let variable.
27+
28+
/*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;
33+
34+
/* e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
35+
replaceALL throwout all the commas from the string and number became numerical value.
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const movieLength = 8784; // length of movie in seconds
2-
1+
const movieLength = 8784;
32
const remainingSeconds = movieLength % 60;
43
const totalMinutes = (movieLength - remainingSeconds) / 60;
54

@@ -8,18 +7,28 @@ const totalHours = (totalMinutes - remainingMinutes) / 60;
87

98
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
109
console.log(result);
10+
//
1111

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+
/* line 1 const movieLength line 3 const remainingSeconds line 4 const totalMinutes
16+
line 6 const remainingMinutes line 7 const totalHours line 9 const result
1517

1618
// b) How many function calls are there?
19+
/* there are one function
20+
console.log(result);
1721

1822
// c) Using documentation, explain what the expression movieLength % 60 represents
1923
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
24+
/*The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
2025
2126
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
27+
/* This expression convert movies time second into minutes dividend by 60 second.
2228
2329
// e) What do you think the variable result represents? Can you think of a better name for this variable?
30+
/* The variable represent the movieLength in second,minutes and hours.
31+
We can change this name as movieTime.
2432

2533
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
34+
/* In this section we can only use the positive natural numbers but if we put numbers that divide by 60 without a remainder get the exact time like 10, 20 50.

‎Sprint-2/3-mandatory-interpret/3-to-pounds.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@ const penceStringWithoutTrailingP = penceString.substring(
44
0,
55
penceString.length - 1
66
);
7-
7+
// remove the p from the "399p" and make "399"
88
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9+
//padStart() add characters to the starting of the string and provide 3 long characters.
10+
//Because we have to convert pound and pence.
911
const pounds = paddedPenceNumberString.substring(
1012
0,
1113
paddedPenceNumberString.length - 2
1214
);
15+
// we take out the 2 character and store the remaining part of the string as the pound.
1316

1417
const pence = paddedPenceNumberString
1518
.substring(paddedPenceNumberString.length - 2)
1619
.padEnd(2, "0");
20+
// At last we get the last two characters as the pence.
1721

1822
console.log(`£${pounds}.${pence}`);
23+
//We can display the price of pound and pence like £3.99.
1924

2025
// This program takes a string representing a price in pence
2126
// The program then builds up a string representing the price in pounds

0 commit comments

Comments
 (0)