Skip to content

Commit c77e1c3

Browse files
interpreting the programs
1 parent 2b4173b commit c77e1c3

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

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

Lines changed: 17 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,27 @@ 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+
//1-carPrice.replaceAll(",", "") in line 4
16+
//3-number(...) in line 4 - enumerate the first result of replaceAll
17+
//2-priceAfterOneYear.replaceAll("," ,"")) in line 5
18+
//4-number(...) in line 5 - enumerate the second result of replaceAll
19+
//5-console.log(..) - line 9
20+
1521

1622
// 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?
23+
// the error is : a syntax error coming from the missing comma in line 5 between the arguments
24+
25+
1726

1827
// c) Identify all the lines that are variable reassignment statements
28+
// const priceDifference = carPrice - priceAfterOneYear; Line 7
29+
// const percentageChange = (priceDifference / carPrice) * 100; Line 8
1930

2031
// d) Identify all the lines that are variable declarations
32+
// carPrice = Number(carPrice.replaceAll(",", "")); Line 4
33+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); Line 5
2134

2235
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
36+
// the number function convert the enumerate a string to a number and the method replaceAll replace ever comma with a space
37+
// the purpose is to clean the value so that it can be converted to a proper number using Number().
38+

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const movieLength = 8784; // length of movie in seconds
1+
2+
const movieLength = -8789; // length of movie in seconds
23

34
const remainingSeconds = movieLength % 60;
45
const totalMinutes = (movieLength - remainingSeconds) / 60;
@@ -12,14 +13,29 @@ console.log(result);
1213
// For the piece of code above, read the code and then answer the following questions
1314

1415
// a) How many variable declarations are there in this program?
16+
// There are 5 variable declarations
1517

1618
// b) How many function calls are there?
19+
// There are 4 function calls in line 9 and 10
1720

1821
// c) Using documentation, explain what the expression movieLength % 60 represents
1922
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
23+
// % is a modulo operator that return the remainder of a multiplied number
2024

2125
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
26+
// movieLength subtract the left over seconds from the total seconds(movieLength) to get a number that is divisible by 60
27+
// when dividing by 60 would convert the remaining seconds into whole minutes which ensures there is no decimal in totalMinutes
28+
2229

2330
// e) What do you think the variable result represents? Can you think of a better name for this variable?
31+
// results present length of movie formatted as Hours:Minutes:Seconds and a better name could be movieDuration
32+
2433

2534
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
35+
36+
// whole positive value: for the 8789 seconds the formatted duration would be fine 2:26:29 and the code would be working as expected
37+
// under 60 seconds: 59 seconds would assign the hours and minutes both as 0s resulting 0:0:59 instead of the normal formatted way 00:00:59 but still correct
38+
// zero value: the formatted duration would also be correct 0:0:0 even though it is an unusual value it still show that it can handle it
39+
// Negative value: for the -8789 seconds would give us an output of -2:-26:-29 which is not a valid format and doesn't make any sense
40+
// the logic breaks where we need to give the user a message to enter a valid number
41+
//

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,23 @@ console.log(`£${pounds}.${pence}`);
2323
// You need to do a step-by-step breakdown of each line in this program
2424
// Try and describe the purpose / rationale behind each step
2525

26+
2627
// To begin, we can start with
2728
// 1. const penceString = "399p": initialises a string variable with the value "399p"
29+
30+
// 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1); reassigned a new value
31+
// using the method substring starting form the index 0 ="3" and ending with -1 exclusively meaning doesn't include 'p'
32+
// resulting with a numeric part of the price which is '399'
33+
34+
//3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
35+
// ensure the number in a 3 digits format by adding zeros minding the consistency of the formatting.
36+
37+
//4.const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);
38+
//extracts everything except the last two number to form the pound portion of the price
39+
40+
//5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
41+
//extracts the last two digits to form the pence portion of the price and the padEnd method ensure the number in a two digits format consistent
42+
43+
//6.console.log(`£${pounds}.${pence}`);
44+
//Combines everything into the final formatted price string. Result: £3.99
45+
//basically the program will take the string "399p" and print out a string of "£3.99"

0 commit comments

Comments
 (0)