Skip to content

Commit bb5c61c

Browse files
committed
Merge branch 'coursework/sprint-3' into coursework/sprint-1
2 parents 85472fe + 5250a3b commit bb5c61c

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@ 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+
// 5 function calls
16+
//Line 4: carPrice.replaceAll(",", "")
17+
//Line 4: Number(...) – wraps the above
18+
//Line 5: priceAfterOneYear.replaceAll(",", "") ✅ But this line has a syntax error (we’ll get to it)
19+
//Line 5: Number(...) – wraps the above
20+
//Line 10: console.log(...)
21+
1522

1623
// 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?
24+
//syntax error! On line 5, there's a missing comma in the replaceAll method.
25+
//correct code should be
26+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1727

1828
// c) Identify all the lines that are variable reassignment statements
29+
carPrice = Number(carPrice.replaceAll(",", ""));
30+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1931

2032
// d) Identify all the lines that are variable declarations
33+
let carPrice = "10,000";
34+
let priceAfterOneYear = "8,543";
2135

2236
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
37+
// This expression converts a string with commas into a number that can be used for mathematical calculations:
38+
// 1. carPrice.replaceAll(",", "") - removes all commas from the string "10,000" → "10000"
39+
// 2. Number(...) - converts the cleaned string "10000" into the number 10000
40+
// Purpose: Strings with commas cannot be used in math operations, so we need to clean and convert them to numbers first

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,33 @@ 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 variable declarations
16+
//Line 1: movieLength
17+
//Line 3: remainingSeconds
18+
//Line 4: totalMinutes
19+
//Line 6: remainingMinutes
20+
//Line 7: totalHours
21+
//Line 9: result
1522

1623
// b) How many function calls are there?
24+
// 1 function call
25+
//Line 10: console.log(result)
1726

1827
// c) Using documentation, explain what the expression movieLength % 60 represents
1928
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
29+
// The % operator is the remainder operator. It returns the remainder left over when one operand is divided by a second operand.
30+
// In this case, movieLength % 60 calculates the number of seconds remaining after dividing the total movie length by 60 (the number of seconds in a minute).
31+
// For example, if movieLength is 8784 seconds, then 8784 % 60 equals 24, meaning there are 24 seconds left over after accounting for full minutes.
2032

2133
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
34+
// The expression (movieLength - remainingSeconds) / 60 calculates the total number of minutes in the movie by first removing the remaining seconds and then dividing by 60 (the number of seconds in a minute).
2235

2336
// e) What do you think the variable result represents? Can you think of a better name for this variable?
37+
// The variable result represents the formatted string output of the total hours, remaining minutes, and remaining seconds of the movie.
38+
// A better name for this variable could be formattedMovieLength.
2439

2540
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
41+
// The code will work for all non-negative integer values of movieLength. It correctly calculates hours, minutes, and seconds for any length of time.
42+
// However, if movieLength is negative or not an integer, the results may not make sense in the context of a movie length.
43+
// For example, if movieLength is negative, the calculations for hours, minutes, and seconds will yield negative values, which are not valid for a movie duration.
44+
// Additionally, if movieLength is a non-integer (e.g., a float), the calculations will still work, but the interpretation of the result may be less clear since movie lengths are typically represented as whole seconds.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ 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+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): removes the trailing 'p' from the string to isolate the numeric part
29+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the numeric string with leading zeros to ensure it is at least 3 digits long
30+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds portion from the padded string
31+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the pence portion and ensures it is 2 digits long
32+
// 6. console.log(`£${pounds}.${pence}`): outputs the final formatted string representing the price in pounds

0 commit comments

Comments
 (0)