Skip to content

Commit 30b52c8

Browse files
committed
Complete 3-mandatory-interpret
1 parent 1bd36fa commit 30b52c8

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

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

Lines changed: 22 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;
@@ -13,10 +13,31 @@ console.log(`The percentage change is ${percentageChange}`);
1313

1414
// a) How many function calls are there in this file? Write down all the lines where a function call is made
1515

16+
// carPrice.replaceAll(",", "") (line 4)
17+
// Number(carPrice.replaceAll(",", "")) (line 4)
18+
// priceAfterOneYear.replaceAll(",", "") (line 5)
19+
// Number(priceAfterOneYear.replaceAll(",", "")) (line 5)
20+
// console.log(`The percentage change is ${percentageChange}`) (line 10)
21+
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?
1723

24+
// Syntax error - Arguments need to be separated by a comma
25+
1826
// c) Identify all the lines that are variable reassignment statements
1927

28+
// carPrice = Number(carPrice.replaceAll(",", "")) (line 4)
29+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")) (line 5)
30+
2031
// d) Identify all the lines that are variable declarations
2132

33+
// let carPrice = "10,000" (line 1)
34+
// let priceAfterOneYear = "8,543" (line 2)
35+
// const priceDifference = carPrice - priceAfterOneYear (line 7)
36+
// const percentageChange = (priceDifference / carPrice) * 100 (line 8)
37+
2238
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
39+
40+
// (carPrice.replaceAll(",", "")) takes the string stored in the carPrice variable ("10,000") and removes all instances of commas (",")
41+
// Without this, the result of Number(carPrice) would be NaN (Not a Number)
42+
43+
// Number(...) then takes the edited string ("10000") and converts it to a number (10000) so that numerical operations can be performed on this value

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,30 @@ console.log(result);
1313

1414
// a) How many variable declarations are there in this program?
1515

16+
// 6
17+
1618
// b) How many function calls are there?
1719

20+
// 1
21+
1822
// c) Using documentation, explain what the expression movieLength % 60 represents
1923
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2024

25+
// It gives the remainder left over when one number is divided by another
26+
2127
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
2228

29+
// Its subtracts the remainder from the movieLength so that it can be divided into a whole number
30+
31+
// (movieLength - remainingSeconds) / 60
32+
// 8760 / 60 = 146
33+
2334
// e) What do you think the variable result represents? Can you think of a better name for this variable?
2435

36+
// The variable represents the movie length in seconds converted to a (HH:MM:SS) format
37+
// A better variable name would be 'formattedTime'
38+
2539
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
40+
41+
// No, this code will not work for all values of movieLength
42+
// In edge cases where movieLength is assigned a value of a negative number or string, the output will not make sense

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

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

4+
// 2. removes the trailing 'p' character
35
const penceStringWithoutTrailingP = penceString.substring(
46
0,
57
penceString.length - 1
68
);
79

10+
// 3. pads the numeric string with leading zeros so it is at least 3 digits
811
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
12+
13+
// 4. extracts the pound part by taking all but the last two digits
914
const pounds = paddedPenceNumberString.substring(
1015
0,
1116
paddedPenceNumberString.length - 2
1217
);
1318

19+
// 5. extracts the pence part by taking the last two digits, and pads with trailing zeroes if needed
1420
const pence = paddedPenceNumberString
1521
.substring(paddedPenceNumberString.length - 2)
1622
.padEnd(2, "0");
1723

24+
// 6. formats and prints the result as a pounds and pence string
1825
console.log(${pounds}.${pence}`);
1926

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

0 commit comments

Comments
 (0)