Skip to content

Commit b8014c0

Browse files
mandatory interpret file requirements have been met
1 parent afa549f commit b8014c0

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

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

Lines changed: 16 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,26 @@ 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
1520

1621
// 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?
22+
// the error is : a syntax error coming from the missing comma in line 5 between the arguments
1723

1824
// c) Identify all the lines that are variable reassignment statements
25+
/* const priceDifference = carPrice - priceAfterOneYear; Line 7
26+
const percentageChange = (priceDifference / carPrice) * 100; Line 8
27+
*/
1928

2029
// d) Identify all the lines that are variable declarations
30+
/* carPrice = Number(carPrice.replaceAll(",", "")); Line 4
31+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); Line 5
32+
*/
2133

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

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@ 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+
//A--> There are 5 variable declarations
1516

1617
// b) How many function calls are there?
18+
//A--> There are 4 function calls in line 9 and 10
1719

18-
// c) Using documentation, explain what the expression movieLength % 60 represents
20+
// c) Usings documentation, explain what the expression movieLength % 60 represents
1921
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
22+
//A--> % is a modulo operator that return the remainder of a multiplied number
2023

2124
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
22-
25+
/* movieLength subtract the left over seconds from the total seconds(movieLength) to get a number that is divisible by 60
26+
when dividing by 60 would convert the remaining seconds into whole minutes which ensures there is no decimal in totalMinutes
27+
*/
2328
// e) What do you think the variable result represents? Can you think of a better name for this variable?
29+
//A--> results present length of movie formatted as Hours:Minutes:Seconds and a better name could be fromatedMovieDuration
30+
2431

2532
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
33+
/*whole positive value: for the 8789 seconds the formatted duration would be fine 2:26:24 and the code would be working as expected
34+
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
35+
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
36+
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
37+
the logic breaks where we need to give the user a message to enter a valid number
38+
*/

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,20 @@ console.log(`£${pounds}.${pence}`);
2525

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

0 commit comments

Comments
 (0)