Skip to content

Commit bb7ccaa

Browse files
committed
Work on 3-mandatory-interpret
1 parent 5cf29fd commit bb7ccaa

3 files changed

Lines changed: 22 additions & 2 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+
//
16+
// 3 function calls at lines:
17+
// 4, 5, 10
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+
// Because there is a missing comma at line 5. By adding the missing comma
1721

1822
// c) Identify all the lines that are variable reassignment statements
23+
// 4, 5
1924

2025
// d) Identify all the lines that are variable declarations
26+
// 1, 2, 7, 8
2127

2228
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
29+
// it replaces all the occurances of "," to "" so essentially deleting commas so it can be turned into a number

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const movieLength = 8784; // length of movie in seconds
1+
const movieLength = 10000; // 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+
// 6
1516

1617
// b) How many function calls are there?
18+
// 1
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+
// it divides the number by 60 so we know how many seconds are remaining by using the modulo operator
2023

2124
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
25+
// this is to convert seconds into minutes without losing any seconds becuase we keep them in remainingSecondds
2226

2327
// e) What do you think the variable result represents? Can you think of a better name for this variable?
28+
// This represents how many hours, minutes and seconds left in the movie. formattedTime.
29+
//
2430

2531
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
32+
// Yeah I think this will work for different inputs, becuase we are using modulo so we're not losing any seconds when dividing

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1+
// initialises a string variable with the value "399p"
12
const penceString = "399p";
23

4+
// This cuts the string so we're only saving everything before the p
35
const penceStringWithoutTrailingP = penceString.substring(
46
0,
57
penceString.length - 1
68
);
79

10+
// This adds zeros to the start of the string if the string's length is less than 3
811
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
12+
// this only saves the string minus the two letters at the end.
913
const pounds = paddedPenceNumberString.substring(
1014
0,
1115
paddedPenceNumberString.length - 2
1216
);
1317

18+
// this extracts the last two characters and adds zeroes to the end
1419
const pence = paddedPenceNumberString
1520
.substring(paddedPenceNumberString.length - 2)
1621
.padEnd(2, "0");
1722

23+
// prints the result
1824
console.log(${pounds}.${pence}`);
1925

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

0 commit comments

Comments
 (0)