diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..8716f0fd4c 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,5 @@ 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 +//the line 3 is updating the value of the count variable. The `=` operator is an assignment operator that takes the value on the right side (which is `count + 1`, meaning the current value of count plus 1) and assigns it to the variable on the left side (which is `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 47561f6175..432704244b 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,7 @@ 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 = ``; +let initials = `"${firstName[0]}${middleName[0]}${lastName[0]}"`; // https://www.google.com/search?q=get+first+character+of+string+mdn - +console.log(initials); // Output: "CKJ" diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28e..9867340e87 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,12 @@ 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 lastDot = filePath.lastIndexOf("."); +const ext = filePath.slice(lastDot); + +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 292f83aabb..f9d76fe129 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,11 @@ 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 + +//step 1: Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive).so it can be any number from 0 up to but not including 1.e.g 0.5, 0.75, 0.25, 0.1, 0.9, etc. and not 1.0. +//step 2: (maximum - minimum + 1) calculates the range of numbers we want to generate. In this case, it calculates the difference between the maximum and minimum values (100 - 1 = 99) and adds 1 to include both endpoints of the range. So, it becomes 100.i.e how many numbers are there from 1 to 100 inclusive.e.g 1,2,3,4,5,6,7,8,9,10,...100. so the range is 100. +//step 3: Math.random() * (maximum - minimum + 1) multiplies the random decimal number generated in step 1 by the range calculated in step 2. This scales the random number to be within the desired range. So, it can be any number from 0 up to but not including 100.e.g 0, 50, 75, 25, 10, 90, etc. and not 100. +//step 4: Math.floor(...) rounds down the result of step 3 to the nearest whole number. This ensures that we get an integer value. so it can be any whole number from 0 up to but not including 100.e.g 0, 50, 75, 25, 10, 90, etc. and not 100. +//step 5: Math.floor(Math.random() * (maximum - minimum + 1)) + minimum adds the minimum value to the result of step 4. This shifts the range of numbers to start from the minimum value. So, it can be any whole number from 1 up to and including 100.e.g 1, 50, 75, 25, 10, 90, etc. and not 0 or 100. +// In summary, the expression generates a random whole number between the minimum and maximum values (inclusive). In this case, it generates a random whole number between 1 and 100 (inclusive).which is what num represents. + diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f7..26f73db71c 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 7a43cbea76..e57b2e9791 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; +console.log(age); diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..2401f6c4c1 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,12 @@ // 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}`); + +/*console.log(`I was born in ${cityOfBirth}`); +const cityOfBirth = "Bolton";*/ +/* The error is that the variable `cityOfBirth` is being used before it is declared and assigned a value. In JavaScript, variables declared with `const` (or `let`) are not hoisted in the same way as `var`, so you cannot access them before their declaration. To fix this, you should declare and assign the variable before using it in the `console.log` statement:*/ +//The variable were being accessed from the Temporal Dead Zone (TDZ) before it was declared and assigned a value. In JavaScript, variables declared with `const` (or `let`) are not hoisted in the same way as `var`, so you cannot access them before their declaration. To fix this, you should declare and assign the variable before using it in the `console.log` statement: + const cityOfBirth = "Bolton"; + +console.log(`I was born in ${cityOfBirth}`); \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884db..6e75f8fa6b 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,9 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//console.log(typeof cardNumber); +const cardNumberString = cardNumber.toString(); + +const last4Digits = cardNumberString.slice(-4); +console.log(last4Digits); // Output: "4213" // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +11,8 @@ 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 +// I suspect the code doesn't work because the slice method is being called on a number, but slice is a method for strings. Therefore, I predict that the code will throw an error saying that slice is not a function for numbers. + +// i console the typeof cardNumber and it returns number + + diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 5f86c730bc..52517b0dc9 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/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-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e18..1691f31938 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/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,31 @@ 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 5 function calls in this file. The function calls are made on the following lines: +// Line 1: replaceAll() +// Line 2: replaceAll() +// Line 5: Number() +// Line 6: Number() +// Line 9: 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? +// 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., and it's a SyntaxError: missing , The error is due to a missing comma in the replaceAll() method. The correct syntax should be replaceAll(",", ""). To fix this problem, we need to add the missing comma in the replaceAll() method on line 5 // c) Identify all the lines that are variable reassignment statements +// The variable reassignment statements are on the following lines: +// Line 4: carPrice = Number(carPrice.replaceAll(",", "")); +// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,"")); // d) Identify all the lines that are variable declarations +// The variable declaration statements are on the following lines: +// 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; +// A declaration is when a variable is created, using let or const. + + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +/* The expression Number(carPrice.replaceAll(",","")) is converting the string value of carPrice, which contains a comma, into a number. The replaceAll() method is used to remove all commas from the string, and then the Number() function is used to convert the resulting string into a number. This allows for mathematical operations to be performed on the value of carPrice without any issues caused by the presence of commas in the string.*/ +// a comma is missing between the two arguments in the replaceAll() method, which is causing a syntax error. The correct syntax should be replaceAll(",", "")., the programming term that belongs in the blank is call Arguments as they are actual values passed when the function is called: diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d2395587..7af2bb9c58 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,32 @@ 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. The variable declarations are on the following lines: +// Line 1: const movieLength = 8784; +// Line 3: const remainingSeconds = movieLength % 60; +// Line 4: const totalMinutes = (movieLength - remainingSeconds) / 60; +// Line 6: const remainingMinutes = totalMinutes % 60; +// Line 7: const totalHours = (totalMinutes - remainingMinutes) / 60; +// Line 9: const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; + // b) How many function calls are there? +// There is 1 function call in this program. The function call is made on the following line: +// Line 10: console.log(result); + // c) Using documentation, explain what the expression movieLength % 60 represents +// The expression movieLength % 60 represents the remainder of the division of movieLength by 60. In this case, it calculates the number of seconds remaining after converting the total length of the movie (in seconds) into minutes. The modulo operator (%) is used to find the remainder when one number is divided by another. So, if movieLength is 8784 seconds, movieLength % 60 will give us the number of seconds that do not make up a full minute. + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The expression assigned to totalMinutes 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 (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. This gives us the total number of complete minutes in the movie length, excluding any remaining 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 string that shows the total length of the movie in hours, minutes, and seconds. A better name for this variable could be "formattedMovieLength" or "movieDuration" to more clearly indicate that it holds the duration of the movie in a human-readable format. // 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, as it correctly calculates the hours, minutes, and seconds for any given length of time in seconds. However, if movieLength is a negative number or a non-integer value, the code may not produce meaningful results. For example, if movieLength is negative, the calculations for remainingSeconds, totalMinutes, and totalHours will not make sense in the context of a movie duration. Additionally, if movieLength is a non-integer (like a float), the calculations may yield unexpected results due to how JavaScript handles floating-point arithmetic. Therefore, it's best to ensure that movieLength is a non-negative integer for this code to work correctly. Also, it works correctly only when the movieleng is less than 24 hours, because the code does not account for days. If the movie length exceeds 24 hours, the totalHours variable will continue to increase without resetting after 24, which may not be the desired behavior for representing time in a standard format. */ +// if movieLength = 90000 seconds, that 25 hours , the variable result will be 25:0:0, which is not a standard representation of time. +// leading zeros , the variable result will always output resuls like H:M:S instead of HH:MM:SS, so if the movie length is 1 hour, 5 minutes and 9 seconds, the variable result will be 1:5:9 instead of 01:05:09. \ 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 60c9ace69a..086813fede 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,10 @@ 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 pence string to isolate the numeric value. +//3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): ensures that the numeric value has at least three digits by padding with 3 leading zeros if necessary, .padStart(target Length, padString), adds strings to the start of the string until it reaches the target length, in this case 3, and if the string is already 3 or more characters long, it will not add any padding. +/*4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds portion of the price ,paddedPenceNumberString.length =3, so paddedPenceNumberString.length - 2 = 1, so paddedPenceNumberString.substring(0, 1) = "3" it took characters from index 0 to 1*/ +/*5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the pence portion of the price and ensures it has two digits by padding with a trailing zero if necessary, paddedPenceNumberString.length =3, so paddedPenceNumberString.length - 2 = 1, so paddedPenceNumberString.substring(1) = "99" it took characters from index 1 to the end of the string, then .padEnd(2, "0") adds strings to the end of the string until it reaches the target length, in this case 2, and if the string is already 2 or more characters long, it will not add any padding.*/ + +/*6. console.log(`£${pounds}.${pence}`): outputs the final formatted price in pounds and pence to the console, using template literals to insert the pounds and pence variables into the string. The output will be in the format "£X.YY", where X is the pounds value and YY is the pence value. In this case, it will output "£3.99".*/ + diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feafe..8133bd55b6 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -10,9 +10,13 @@ Let's try an example. In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; + What effect does calling the `alert` function have? +// By invoking alert("Hello world!"); the effect it has is that a small modal dialog box pops up at the top of the window browser displaying Hello world!, along with an OK button. Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. What effect does calling the `prompt` function have? +//The effect is similar to the alert one a small modal dialog box pops up with the addition of a text input field and two buttons for 'OK' and 'Cancel', it displays the question What is your name? What is the return value of `prompt`? +//The return value of the 'prompt' depends on what action the client did , whci is one of two things based on the client actions.it will return whatever the client typed into the text field as string and click 'OK' and this will be stored in the variable myName, and can be verified by typing myName into the console and hitting ENTER.However if the user clicks cancel , it returns null. diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56a..a2119b424f 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -4,13 +4,96 @@ In this activity, we'll explore some additional concepts that you'll encounter i Open the Chrome devtools Console, type in `console.log` and then hit enter -What output do you get? +What output do you get? // I got 'console.log' +// ƒ log() { [native code] } Now enter just `console` in the Console, what output do you get back? +/*console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} +assert +: +ƒ assert() +clear +: +ƒ clear() +context +: +ƒ context() +count +: +ƒ count() +countReset +: +ƒ countReset() +createTask +: +ƒ createTask() +debug +: +ƒ debug() +dir +: +ƒ dir() +dirxml +: +ƒ dirxml() +error +: +(...n)=> {…} +group +: +ƒ group() +groupCollapsed +: +ƒ groupCollapsed() +groupEnd +: +ƒ groupEnd() +info +: +ƒ info() +log +: +ƒ log() +memory +: +MemoryInfo {totalJSHeapSize: 33100000, usedJSHeapSize: 29400000, jsHeapSizeLimit: 3760000000} +profile +: +ƒ profile() +profileEnd +: +ƒ profileEnd() +table +: +ƒ table() +time +: +ƒ time() +timeEnd +: +ƒ timeEnd() +timeLog +: +ƒ timeLog() +timeStamp +: +ƒ timeStamp() +trace +: +(...n)=> {…} +warn +: +(...n)=> {…} +Symbol(Symbol.toStringTag) +: +"console"*/ Try also entering `typeof console` +// got: object Answer the following questions: What does `console` store? +/* Console is a globally available Object provided by the browser environment to store debugging tools functions in one convienient place*/ What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +/*`console.log` or `console.assert` mean? i.e console.log go inside the console Object and print out whatever you have on the console to see, log() is a method which does the action on the object , and for console.assert means check whether something is true and if it's not true show the error message on the console. the '.' is called the property accessor or dot Notation and what it does is to dig inside an object and grab a specific piece of data or functionality stored within it.*/