Skip to content

Commit e7bdeb9

Browse files
author
Arthur
committed
latest part 3
1 parent 3d48584 commit e7bdeb9

3 files changed

Lines changed: 60 additions & 6 deletions

File tree

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

Lines changed: 21 additions & 2 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,30 @@ 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+
6
1516

1617
// 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?
18+
It lacks comma insides the replaceAll.
1719

18-
// c) Identify all the lines that are variable reassignment statements
1920

21+
// c) Identify all the lines that are variable reassignment statements
22+
carPrice = Number(carPrice.replaceAll(",", ""));
23+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
2024
// d) Identify all the lines that are variable declarations
25+
let carPrice = "10,000";
26+
let priceAfterOneYear = "8,543";
27+
28+
const priceDifference = carPrice - priceAfterOneYear;
29+
const percentageChange = (priceDifference / carPrice) * 100;
2130

2231
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
32+
It is to take out the comma insides the number of carPrice.
33+
34+
35+
36+
37+
38+
39+
40+
41+

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

Lines changed: 10 additions & 3 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 = 6; // length of movie in seconds
22

33
const remainingSeconds = movieLength % 60;
44
const totalMinutes = (movieLength - remainingSeconds) / 60;
@@ -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-
15+
//6
1616
// b) How many function calls are there?
17-
17+
//1
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
2020

21+
//It divides totalMinutes into 60, see how much remainingMinutes left.
22+
2123
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
24+
//After deducting the totalMinutes, and divide the second. How much do he have the totalMinutes ?
2225

2326
// e) What do you think the variable result represents? Can you think of a better name for this variable?
27+
//The total duration of the movieLength.
2428

2529
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
30+
//for one hour = 3600 minutes . 1:00:00
31+
//for 59 minutes = 3540 minutes . 0:59:0
32+
//for seconds = 6 seconds . 0:0:6

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,32 @@ console.log(`£${pounds}.${pence}`);
2424
// Try and describe the purpose / rationale behind each step
2525

2626
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
27+
1. const penceString = "399p": initialises a string variable with the value "399p"
28+
2.
29+
const penceStringWithoutTrailingP = penceString.substring(
30+
0,
31+
penceString.length - 1
32+
); the purpose of this is to take out the p.
33+
34+
35+
3.
36+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
37+
if it is shorter than 3 characters, the 0 will add in front of the number ; but 399 is already 3 numbers so we donot need to do anything.
38+
39+
40+
41+
4. const pounds = paddedPenceNumberString.substring(
42+
0,
43+
paddedPenceNumberString.length - 2
44+
); the length of the string is 3 , after -2 , it will become 1. The const pounds will take out 3 and it will become 3.
45+
46+
47+
5. const pence = paddedPenceNumberString
48+
.substring(paddedPenceNumberString.length - 2)
49+
.padEnd(2, "0");
50+
51+
It will take out the number from the end to 1 ; so it will have 99.
52+
53+
6.console.log(${pounds}.${pence}`);
54+
55+
Finally it will put the pounds and the pence into the template and print it out. The result will be £${3}.${99} ;

0 commit comments

Comments
 (0)