Skip to content

Commit d1371b0

Browse files
committed
Interpret part completed
1 parent 920f815 commit d1371b0

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

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

Lines changed: 10 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,20 @@ 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 4 function calls.
16+
// In line 4: Number() and replaceAll() are function calls.
17+
// In line 5: Number() and replaceAll() are function calls.
18+
// Including console.log(), there are 5 function calls.
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 syntax error was in line 5; a comma was missing
1722

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

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

2229
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
30+
// First, it removes the digit group separator. // "10,000" => "10000"
31+
// Then, the Number() method changes the string to a number. // "10000" => 10000

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ 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
1516

1617
// b) How many function calls are there?
18+
// There are no function calls except 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+
// 'movieLength % 60' finds out the remainder seconds which is 24
2023

2124
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
25+
// It subtracts the remaining seconds from movieLength and finally divides by 60 to get the length in minutes
2226

2327
// e) What do you think the variable result represents? Can you think of a better name for this variable?
28+
// It stores the total movie length in HH:MM:SS format.
29+
// I can name it totalRuntime;
2430

2531
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
32+
// Yes, the code works for all values. 6000 would be 1:40:0, for example

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
const penceString = "399p";
22

3+
// substring() method returns a portion of a string
4+
// In the variable below, it returns "399" and penceString.length - 1 removes the last character ("p")
35
const penceStringWithoutTrailingP = penceString.substring(
46
0,
57
penceString.length - 1
68
);
79

10+
// Although pasStart() method does not seem necessary here, but this method gives a padding to a string from start
11+
// so the "399" has a given length of 3
812
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
13+
14+
// substring() method returns a portion of a string
15+
// In this case, it return only "3" which is the Pound
16+
// and 'paddedPenceNumberString.length - 2' removes "99" which represents the pence
917
const pounds = paddedPenceNumberString.substring(
1018
0,
1119
paddedPenceNumberString.length - 2
1220
);
1321

22+
// substring() method removes "3" from "399" and returns "99";
23+
// padEnd() method pads the "99" and the padding applied is from the end of this string.
1424
const pence = paddedPenceNumberString
1525
.substring(paddedPenceNumberString.length - 2)
1626
.padEnd(2, "0");
1727

28+
29+
// In the log, backticks are used to output template literals ${pounds} and ${pence} with added '£' sign.
1830
console.log(${pounds}.${pence}`);
1931

2032
// This program takes a string representing a price in pence

0 commit comments

Comments
 (0)