diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..9e0b1949a 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,11 +12,29 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made +// 5 function calls +//Line 4: carPrice.replaceAll(",", "") +//Line 4: Number(...) – wraps the above +//Line 5: priceAfterOneYear.replaceAll(",", "") ✅ But this line has a syntax error (we’ll get to it) +//Line 5: Number(...) – wraps the above +//Line 10: console.log(...) + // 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? +//syntax error! On line 5, there's a missing comma in the replaceAll method. +//correct code should be +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // c) Identify all the lines that are variable reassignment statements +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // d) Identify all the lines that are variable declarations +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// This expression converts a string with commas into a number that can be used for mathematical calculations: +// 1. carPrice.replaceAll(",", "") - removes all commas from the string "10,000" → "10000" +// 2. Number(...) - converts the cleaned string "10000" into the number 10000 +// Purpose: Strings with commas cannot be used in math operations, so we need to clean and convert them to numbers first diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..4f084a25d 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,33 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? +// 6 variable declarations +//Line 1: movieLength +//Line 3: remainingSeconds +//Line 4: totalMinutes +//Line 6: remainingMinutes +//Line 7: totalHours +//Line 9: result // b) How many function calls are there? +// 1 function call +//Line 10: console.log(result) // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// The % operator is the remainder operator. It returns the remainder left over when one operand is divided by a second operand. +// 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). +// For example, if movieLength is 8784 seconds, then 8784 % 60 equals 24, meaning there are 24 seconds left over after accounting for full minutes. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// 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). // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result represents the formatted string output of the total hours, remaining minutes, and remaining seconds of the movie. +// A better name for this variable could be formattedMovieLength. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// The code will work for all non-negative integer values of movieLength. It correctly calculates hours, minutes, and seconds for any length of time. +// However, if movieLength is negative or not an integer, the results may not make sense in the context of a movie length. +// 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. +// 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. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..c42e55592 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,8 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): removes the trailing 'p' from the string to isolate the numeric part +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the numeric string with leading zeros to ensure it is at least 3 digits long +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds portion from the padded string +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the pence portion and ensures it is 2 digits long +// 6. console.log(`£${pounds}.${pence}`): outputs the final formatted string representing the price in pounds