diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..42f16c337 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -2,5 +2,10 @@ let count = 0; count = count + 1; +console.log(count); // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing + +// Answer +// Line 3 is an assignment operation. The = operator assigns the value of the expression on its right +// (count + 1) to the variable on its left (count). This effectively increments the value of count by 1. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..116b24c99 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -4,8 +4,9 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. +// +let initials = firstName[0] + middleName[0] + lastName[0]; -let initials = ``; - +console.log(initials); // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..dc0e222b2 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,9 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; +const dir = filePath.slice(0, lastSlashIndex); +const ext = filePath.slice(filePath.lastIndexOf(".") + 1); +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..baa81c1c3 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,9 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing +// Answer +// The expression generates a random integer between the minimum and maximum values (inclusive). +// Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). +// Multiplying this by (maximum - minimum + 1) scales it to the range of possible integers. +// Adding minimum shifts the range to start from the minimum value. +// Finally, Math.floor() rounds down to the nearest whole number, ensuring that num is an integer within the specified range. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..65ad3030d 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +//This is just an instruction for the first activity - but it is just for human consumption +//We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..031839b47 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,4 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..2865eb8a4 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,5 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..047e0302c 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -7,3 +7,6 @@ const last4Digits = cardNumber.slice(-4); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value +//answer +const cardNumber = 4533787178994213; +const last4DigitsString = cardNumber.toString().slice(-4); \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..f0ab94e12 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,2 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..10a380cfd 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -13,10 +13,28 @@ console.log(`The percentage change is ${percentageChange}`); // a) How many function calls are there in this file? Write down all the lines where a function call is made +// 1. carPrice.replaceAll(",", "") +// 2. priceAfterOneYear.replaceAll(",", "") +// 3. Number(...) +// 4. console.log(...) - +// 5. (priceDifference / carPrice) * 100 - line 7 (this is an expression that involves division and multiplication, but it does not involve a function call) + // 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? +// 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. +// Fix: Change `priceAfterOneYear.replaceAll(",", "")` to `priceAfterOneYear.replaceAll(",", "")` + // c) Identify all the lines that are variable reassignment statements +// Line 4: carPrice = Number(carPrice.replaceAll(",", "")); +// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + // d) Identify all the lines that are variable declarations +// Line 1: let carPrice = "10,000"; +// Line 2: let priceAfterOneYear = "8,543"; +// Line 7: const priceDifference = carPrice - priceAfterOneYear; +// Line 8: const percentageChange = (priceDifference / carPrice) * 100; + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// 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. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..2955739d8 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,20 @@ 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? +// There are 6 variable declarations in this program: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result. // b) How many function calls are there? +// There is 1 function call in this program: 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 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. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// 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. // 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 time in hours, minutes, and seconds. A better name for this variable could be `formattedTime`. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// 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. \ 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..fb05325dc 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -18,10 +18,36 @@ const pence = paddedPenceNumberString console.log(`£${pounds}.${pence}`); // This program takes a string representing a price in pence +// It then removes the trailing "p" character, pads the number with leading +// zeros to ensure it has at least 3 digits, and then separates the pounds and pence parts of the number. + // The program then builds up a string representing the price in pounds +// 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. + +// 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"). +// by concatenating the pounds and pence parts together with a "£" symbol and a decimal point. The final output is printed to the console. // You need to do a step-by-step breakdown of each line in this program +// a) How many function calls are there in this file? Write down all the lines where a function call is made +// 1. penceString.substring(0, penceString.length - 1) - line 3 +// 2. penceStringWithoutTrailingP.padStart(3, "0") - line 5 +// 3. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) - line 6 +// 4. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) - line 9 +// 5. .padEnd(2, "0") - line 9 +// 6. console.log(`£${pounds}.${pence}`)' - line 11 + // Try and describe the purpose / rationale behind each step +// 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? +// c) Identify all the lines that are variable reassignment statements +// d) Identify all the lines that are variable declarations +// e) Describe what the expression penceString.substring(0, penceString.length - 1) is doing - what is the purpose of this expression? // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +// a) How many function calls are there in this file? Write down all the lines where a function call is made + +// 1. penceString.substring(0, penceString.length - 1) - line 3 +// 1. const penceString = "399p": initialises a string variable with the val +// ue "399p". +// 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"). +// 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"). +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length -