Skip to content

Commit 9f7fab4

Browse files
committed
fix: resolve mandatory errors and complete interpret exercises
1 parent 6c20163 commit 9f7fab4

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

Sprint-1/2-mandatory-errors/4.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
const 12HourClockTime = "8:53pm";
2-
const 24hourClockTime = "20:53";
1+
const HourClockTime = "8:53pm";
2+
const hourClockTime = "20:53";
3+
4+
// Naming convention: In JavaScript, variable names cannot begin with a number.
5+
// It would crash instantly with a syntax error:

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,8 @@ 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("," ""));
6+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));
67

78
const priceDifference = carPrice - priceAfterOneYear;
89
const percentageChange = (priceDifference / carPrice) * 100;
@@ -20,3 +21,18 @@ console.log(`The percentage change is ${percentageChange}`);
2021
// d) Identify all the lines that are variable declarations
2122

2223
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
24+
25+
/*
26+
a) There are two functions Line 4 & 5:
27+
for eg, The expression Number(carPrice.replaceAll(",","")) is doing the following:
28+
1. carPrice.replaceAll(",","") - This removes any commas from the carPrice string.
29+
2. Number(...) - This converts the resulting string into a number.
30+
b) The uncaught syntax error is coming from line 5. The error occurs because there is a missing comma in the replaceAll method.
31+
It should be carPrice.replaceAll(",", "") instead of carPrice.replaceAll("," ""). To fix this, add the missing comma.
32+
c) The variable reassignment statements are on lines 4 and 5, where carPrice and priceAfterOneYear are being reassigned to their numeric values.
33+
d) The variable declarations are on lines 1 and 2, where carPrice and priceAfterOneYear are declared using the let keyword.
34+
35+
e) The purpose of the expression Number(carPrice.replaceAll(",", "")) is to convert a string representation of a number (with commas as thousands separators) into an actual number that can be used in mathematical operations.
36+
37+
Generally, the purpose of this expression is to convert a string representation of a number (with commas as thousands separators) into a actual number that can be used in mathematical operations.
38+
*/

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,17 @@ console.log(result);
2323
// e) What do you think the variable result represents? Can you think of a better name for this variable?
2424

2525
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
26+
27+
/*
28+
a) There are 6 variable declarations in this program: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result.
29+
30+
b) There is 1 function call in this program: console.log(result).
31+
32+
c) The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. This is used to determine the number of seconds that do not make up a full minute.
33+
34+
d) The expression assigned to totalMinutes calculates the total number of minutes in the movie by subtracting the remaining seconds from the total length of the movie and then dividing by 60. This gives the whole number of minutes.
35+
e) The variable result represents the formatted string of the movie length in hours, minutes, and seconds. A better name for this variable could be movieDuration.
36+
37+
f) The code will work for all non-negative values of movieLength. However, if movieLength is negative, the calculations for remainingSeconds, totalMinutes, remainingMinutes, and totalHours may not produce meaningful results.
38+
Additionally, if movieLength is not an integer, the calculations may also end up with unexpected results.
39+
*/

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): this removes the last character of the string (the "p"), leaving just the digits "399".
29+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): this pads the string with leading zeros until it is at least 3 characters long. Since "399" is already 3 characters, it stays "399". This step matters for smaller pence values, e.g. "9p" would become "009", ensuring there are always at least 2 digits left for pence.
30+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): this takes all characters except the last 2, giving the pounds part of the amount. For "399", this gives "3".
31+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): this takes the last 2 characters of the padded string (the pence part), then pads it with trailing zeros if it's shorter than 2 characters. For "399", this gives "99".
32+
// 6. console.log(£${pounds}.${pence}): this combines the pounds and pence parts into a formatted price string, e.g. "£3.99".

0 commit comments

Comments
 (0)