diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..65617f6c7 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -4,3 +4,9 @@ count = count + 1; // 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 + +Line 3 updated the value of the variable "count". The "=" operator is an +assignment operator, which takes the value on the right side +("count + 1") and assigns it to the variable on the left side +("count"). So, it takes the current value of "count", adds 1 to it, and then +stores that new value back into "count".. diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..efa1867f7 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,7 @@ const 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. -const initials = ``; +const initials = firstName[0] + middleName[0] + lastName[0]; + // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..b96e4cd6a 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,7 @@ 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 = base.slice(base.lastIndexOf(".") + 1); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..779752f49 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -7,3 +7,21 @@ 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 + +num //represents a random integer between the minimum value (1) and the maximum value (100).Math + +(maximum - minimum + 1) //represents the range of possible values, which is 100 - 1 + 1 = 100. +// This means that there are 100 possible values that num can take on. + +Math.random() //generates a random decimal number between 0 and 1. This means that the value of Math.random() +// can be any number between 0 and 1, including 0 but not including 1. + +Math.floor(Math.random() * (maximum - minimum + 1)) //multiplies the random decimal number generated by Math.random() +// by the range of possible values (100), and then rounds down to the nearest integer using Math.floor(). +// This means that the result will be a random integer between 0 and 99. + +Math.floor(Math.random() * (maximum - minimum + 1)) + minimum //adds the minimum value (1) to the result of the previous +// expression. This means that the final result will be a random integer between 1 and 100, inclusive. + + + diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..65ad3030d 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/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-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..031839b47 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/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-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..c441b2e77 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,6 @@ // 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-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..8dc2a595b 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,9 +1,22 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Before running the code, make and explain a prediction about why the code won't work + +The code is not working because the "slice" method is being used incorrectly. The "slice" +method should be called on the string "cardNumber" to extract the last 4 digits. However, +if there is a syntax error or if the variable "cardNumber" is not defined correctly, it could lead to an error. + // 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? +It gives an error because the "slice" method is being called on a string, but if the string is not defined correctly or +if there is a typo in the variable name, it will result in an error. This is what I predicted, as +I anticipated that the issue would be related to how the "slice" method is being used on the string. + // Then try updating the expression last4Digits is assigned to, in order to get the correct value + +I updated the expression to correctly use the "slice" method on the "cardNumber" string. I converted the cardNumber to a +string with the ("") and then used the slice method to extract the last 4 digits. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..52517b0dc 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,2 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const twelveHourClockTime = "8:53pm"; +const twentyFourHourClockTime = "20:53"; \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..5728478a2 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,11 +12,34 @@ 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 +//There are five function calls in this file. The lines where function calls are made are: +//1. Line 4: carPrice.replaceAll(",", "") +//2. Line 5: priceAfterOneYear.replaceAll(",", "") +//3. Line 7: Number(carPrice.replaceAll(",", "")) +//4. Line 8: Number(priceAfterOneYear.replaceAll(",", "")) +//5. 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? +//The error is occurring on line 5 because a comma is missing between the arguments in the replaceAll function, to fix the problem, + //we need to include the "," character in the replaceAll function like this: priceAfterOneYear.replaceAll(",", "") + // c) Identify all the lines that are variable reassignment statements -// d) Identify all the lines that are variable declarations +//The lines that are variable reassignment statements are: +//1. Line 4: carPrice = Number(carPrice.replaceAll(",", "")); +//2. Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); +// d) Identify all the lines that are variable declarations +//The lines that are variable declarations are: +//1. Line 1: let carPrice = "10,000"; +//2. Line 2: let priceAfterOneYear = "8,543"; +//3. Line 6: const priceDifference = carPrice - priceAfterOneYear; +//4. Line 7: const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +//The expression "Number(carPrice.replaceAll(",", ""))" is performing two main operations: +//1. "carPrice.replaceAll(",", "")": This part of the expression removes all commas from the string representation of +//"carPrice". For example, it converts "10,000" to "10000". +//2. "Number(priceAfterOneYear.replaceAll(",", ""));": This part converts the resulting string (after removing commas) +//into a numeric value. The purpose of this expression is to convert the string representation of a number that +//may contain commas into an actual numeric value that can be used for mathematical calculations. diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..57d3bc041 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = -9876; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -11,15 +11,41 @@ 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 six variable declarations in this program. The lines where variable declarations are made: +//1. Line 1: const movieLength = 8784; +//2. Line 3: const remainingSeconds = movieLength % 60; +//3. Line 4: const totalMinutes = (movieLength - remainingSeconds) / 60; +//4. Line 6: const remainingMinutes = totalMinutes % 60; +//5. Line 7: const totalHours = (totalMinutes - remainingMinutes) / 60; +//6. Line 9: const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; // b) How many function calls are there? +//There are two function calls in this program. The lines where function calls are made are: +//1. Line 9: `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +//2. 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 expression "movieLength % 60" calculates the remainder when the value of "movieLength" is divided by 60. +//In this context, it is used to determine the number of seconds remaining after converting the total movie +//length from seconds into minutes. Since there are 60 seconds in a minute, this operation effectively gives us the +//leftover seconds that do not make up a full minute. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +//The expression assigned to "totalMinutes" in line 4 calculates the total number of minutes in the movie length. +//It does this by first subtracting the remaining seconds (calculated in line 3) from the total movie length in seconds. +//This gives the total number of seconds that can be fully converted into minutes. Then, it divides that value by 60 to +//convert the total seconds into total minutes. The result is the total number of complete minutes in the movie length, +//excluding any leftover seconds. // 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 string in the format "hours:minutes: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 +//The code will work for all positive integer values of movieLength, as it correctly calculates the hours, minutes, +//and seconds based on the total length in seconds. But will not work for negative values of movieLength, +//as it will produce negative hours, minutes, and seconds, which does not make sense in the context of a movie length. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..b1cd98701 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,9 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +// 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, resulting in "399" +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the string with leading zeros to ensure it has at least 3 characters, resulting in "399" +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds part of the string by taking all characters except the last two, resulting in "3" +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the pence part of the string by taking the last two characters and pads it with trailing zeros if necessary, resulting in "99" +// 6. console.log(`£${pounds}.${pence}`): outputs the final formatted price in pounds and pence, resulting in "£3.99"