Skip to content

Commit 8b28453

Browse files
author
Pretty Taruvinga
committed
. fixed a syntax error
.corrected replaceAll usage . made the code run . answered the analysis questions
1 parent 3372770 commit 8b28453

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,26 @@ 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+
// 1. carPrice.replaceAll(",", "")
17+
// 2. priceAfterOneYear.replaceAll(",", "")
18+
// 3. Number(...)
19+
// 4. console.log(...) -
20+
// 5. (priceDifference / carPrice) * 100 - line 7 (this is an expression that involves division and multiplication, but it does not involve a function call)
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+
// The error is on line 5 where `priceAfterOneYear.replaceAll(",", "")` is called. The syntax error is due to a missing closing quote in the `replaceAll` method.
25+
// Fix: Change `priceAfterOneYear.replaceAll(",", "")` to `priceAfterOneYear.replaceAll(",", "")`
26+
1827
// c) Identify all the lines that are variable reassignment statements
1928

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

34+
// Line 1: let carPrice = "10,000";
35+
// Line 2: let priceAfterOneYear = "8,543";
36+
2237
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
38+
// The expression `carPrice.replaceAll(",", "")` removes all commas from the string `carPrice`, converting it to a number that can be used in mathematical operations. This is necessary because the original value is a string with commas (e.g., "10,000"), which cannot be directly used in arithmetic calculations. The `Number()` function then converts the resulting string (e.g., "10000") into a numeric value.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ 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+
// There are 6 variable declarations in this program: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result.
1516

1617
// b) How many function calls are there?
18+
// There is 1 function call in this program: console.log(result);
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+
// The expression `movieLength % 60` calculates the remainder when `movieLength` is divided by 60. This is used to determine the number of seconds that are left after accounting for the full minutes in the movie length. For example, if `movieLength` is 8784 seconds, then `movieLength % 60` would give us the remaining seconds after converting as many full minutes as possible from the total seconds.
2023

2124
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
25+
// The expression assigned to `totalMinutes` is calculating the total number of full minutes in the movie length. It does this by first subtracting the `remainingSeconds` from `movieLength`, which gives us the total number of seconds that can be fully converted into minutes. Then, it divides that result by 60 to convert those seconds into minutes. For example, if `movieLength` is 8784 seconds and `remainingSeconds` is 24 seconds, then `totalMinutes` would be calculated as (8784 - 24) / 60, which equals 145 minutes.
2226

2327
// e) What do you think the variable result represents? Can you think of a better name for this variable?
28+
// The variable `result` represents the formatted time in hours, minutes, and seconds. A better name for this variable could be `formattedTime`.
2429

2530
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
31+
// This code will work for all non-negative integer values of `movieLength`. It correctly calculates the hours, minutes, and seconds for any given length of time in seconds. However, if `movieLength` is negative, the calculations would not make sense in the context of a movie length, and the output would be incorrect. Therefore, it is important to ensure that `movieLength` is a non-negative integer for this code to function properly.

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,36 @@ const pence = paddedPenceNumberString
1818
console.log(${pounds}.${pence}`);
1919

2020
// This program takes a string representing a price in pence
21+
// It then removes the trailing "p" character, pads the number with leading
22+
// zeros to ensure it has at least 3 digits, and then separates the pounds and pence parts of the number.
23+
2124
// The program then builds up a string representing the price in pounds
25+
// and pence format (e.g., "£3.99") by concatenating the pounds and pence parts together with a "£" symbol and a decimal point. The final output is printed to the console.
26+
27+
// This program takes a string representing a price in pence (e.g., "399p") and converts it into a formatted string representing the price in pounds and pence (e.g., "£3.99").
28+
// by concatenating the pounds and pence parts together with a "£" symbol and a decimal point. The final output is printed to the console.
2229

2330
// You need to do a step-by-step breakdown of each line in this program
31+
// a) How many function calls are there in this file? Write down all the lines where a function call is made
32+
// 1. penceString.substring(0, penceString.length - 1) - line 3
33+
// 2. penceStringWithoutTrailingP.padStart(3, "0") - line 5
34+
// 3. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) - line 6
35+
// 4. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) - line 9
36+
// 5. .padEnd(2, "0") - line 9
37+
// 6. console.log(`£${pounds}.${pence}`)' - line 11
38+
2439
// Try and describe the purpose / rationale behind each step
40+
// 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?
41+
// c) Identify all the lines that are variable reassignment statements
42+
// d) Identify all the lines that are variable declarations
43+
// e) Describe what the expression penceString.substring(0, penceString.length - 1) is doing - what is the purpose of this expression?
2544

2645
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
46+
// a) How many function calls are there in this file? Write down all the lines where a function call is made
47+
48+
// 1. penceString.substring(0, penceString.length - 1) - line 3
49+
// 1. const penceString = "399p": initialises a string variable with the val
50+
// ue "399p".
51+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable by taking a substring of `penceString` that excludes the last character (the "p"). This effectively removes the trailing "p" from the original string, leaving just the numeric part (e.g., "399").
52+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the `penceStringWithoutTrailingP` with leading zeros to ensure it has at least 3 characters. If `penceStringWithoutTrailingP` is shorter than 3 characters, it will add zeros at the start until it reaches a length of 3 (e.g., "399" remains "399", but "99" would become "099").
53+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length -

0 commit comments

Comments
 (0)