diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..79e293e3f 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,8 @@ 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 is updating the value of the count variable by adding 1 to its current value. +// The = operator assigns the new value back to the count variable. + +console.log(count); +// Expected output: 1 diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..589d571df 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -4,8 +4,17 @@ 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. +const index = 0; +let initials = firstName.charAt(index) + + middleName.charAt(index) + + lastName.charAt(index); -let initials = ``; +console.log(`The first letters of ${firstName}'s name are ${initials}`); +// Expected output: "CKJ" // https://www.google.com/search?q=get+first+character+of+string+mdn + + +console.log(firstName[0] + middleName[0] + lastName[0]); +// Expected output: "CKJ \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..30172b193 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,10 @@ 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(".")); + +console.log(`The ext part of ${filePath} is ${ext}`); +console.log(`The dir part of ${filePath} is ${dir}`); // 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..84f2289ca 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,12 @@ 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 + +console.log(num); + +// Explanation: +// 1. Math.random() generates a random floating-point number between 0 (inclusive) and 1 (exclusive) [0, 1) . +// 2. Multiplying this random number by (maximum - minimum + 1) scales it to a range of 0 (inclusive) to (maximum - minimum + 1) (exclusive). +// 3. Math.floor() then rounds this scaled number down to the nearest whole number, resulting in an integer from 0 to (maximum - minimum). +// 4. Finally, adding 'minimum' shifts this range up, resulting in a final value between 'minimum' and 'maximum' (both inclusive). +// Therefore, 'num' represents a random integer between 1 and 100, inclusive. diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..8cf6badac 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,4 @@ -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? +- So we can make a multi-line comment or a single-line comment */ + diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..c0399b8af 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,6 @@ // 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); +// The code above works fine. It was an error because of using 'const' instead of 'let' diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..5f2a53799 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/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}`); +// The error was an order error - declaration must come before usage \ 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 ec101884d..070f70221 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,22 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); - +// const last4Digits = cardNumber.slice(-4); // 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 // 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 + + + +// TypeError: cardNumber.slice is not a function +// The error occurs because cardNumber is a number, and the slice() method is not defined for numbers. +// The slice() method is a string method, so to use it, we need to convert cardNumber to a string first. +// To fix the error, we can use the toString() method to convert cardNumber to a string before calling slice(). +// Updated code: +let last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); +last4Digits = Number(last4Digits); +typeof last4Digits; +console.log(`Last four digits of cardNumber are ${last4Digits} and type of this variable is ${typeof last4Digits}`); + diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..c5cc4169c 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,8 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file + +// Variable declaration expected.ts(1134) +// The error occurs because variable names cannot start with a number in JavaScript. +// To fix the error, we can rename the variables to start with a letter or an underscore. +const hourClockTime12 = "20:53"; +const hourClockTimeEight24 = "08:53"; + +console.log(`12-hour clock time is ${hourClockTime12} and 24-hour clock time is ${hourClockTimeEight24}`); \ 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..b290b4f8e 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -1,9 +1,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 +10,28 @@ 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 4 function calls in this file. They are on the following lines: +// Line 3: carPrice.replaceAll(",", "") +// Line 3: Number(carPrice.replaceAll(",", "")) +// Line 4: priceAfterOneYear.replaceAll(",", "") +// Line 4: Number(priceAfterOneYear.replaceAll(",", "")) // 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 code had an error in replaceAll because it wasn't a comma between the quotation marks. // c) Identify all the lines that are variable reassignment statements - +// The variable reassignment statements are on the following lines: +// Line 3: carPrice = Number(carPrice.replaceAll(",", "")); +// Line 4: 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 5: const priceDifference = carPrice - priceAfterOneYear; +// Line 6: 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 tasks: +// 1. carPrice.replaceAll(",","") - This part of the expression is using the replaceAll method to remove all commas from the string value of carPrice. +// This is necessary because commas are not valid in numerical representations and would cause issues when trying to convert the string to a number. +// 2. Number(...) - The outer Number function takes the resulting string (which now has no commas) and converts it into a numerical value. This allows +// for mathematical operations to be performed on carPrice, such as subtraction and percentage calculations later in the code. diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..9fcd894d4 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,25 +1,58 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 86400 ; // length of movie in seconds const remainingSeconds = movieLength % 60; +console.log(remainingSeconds); const totalMinutes = (movieLength - remainingSeconds) / 60; - +console.log(totalMinutes); const remainingMinutes = totalMinutes % 60; +console.log(remainingMinutes); const totalHours = (totalMinutes - remainingMinutes) / 60; - +console.log(totalHours); const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; 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 5 variable declarations in this program. They are on the following lines: +// Line 1: const movieLength = 8784; // length of movie in seconds +// Line 3: const remainingSeconds = movieLength % 60; +// Line 5: const totalMinutes = (movieLength - remainingSeconds) / 60; +// Line 7: const remainingMinutes = totalMinutes % 60; +// Line 9: const totalHours = (totalMinutes - remainingMinutes) / 60; +// Line 11: const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; // b) How many function calls are there? +// There is 1 function call in this program. It is on the following line: +// Line 11: console.log(result); +// Hint: Template literals are not function calls // c) Using documentation, explain what the expression movieLength % 60 represents +// The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. +// The % operator is known as the modulus operator, and it returns the remainder of a division operation. +// In this case, it is used to determine how many seconds are left over after accounting for complete minutes in the total movie length. +// For example, if movieLength is 8784 seconds, dividing by 60 gives 146 minutes with a remainder of 24 seconds. +// Therefore, movieLength % 60 would evaluate to 24. + +// Documentation reference: // 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 is (movieLength - remainingSeconds) / 60. +// This expression calculates the total number of complete minutes in the movie length by first subtracting the remaining seconds from the total movie length (in seconds) +// to get a value that is a multiple of 60 (i.e., the total seconds that can be evenly divided into minutes). +// Then, it divides that result by 60 to convert the total seconds into minutes. +// For example, if movieLength is 8784 seconds and remainingSeconds is 24 seconds, +// then (8784 - 24) equals 8760 seconds, which when divided by 60 gives 146 minutes. +// Therefore, totalMinutes would evaluate to 146. // 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 of the movie length in hours, minutes, and seconds (HH:MM:SS). +// A better name for this variable could be formattedMovieLength or movieDurationFormatted, as these names more clearly indicate that the variable holds a formatted representation of the movie length. // 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, as it correctly calculates the hours, minutes, and seconds for any given length in seconds. +// However, if movieLength is a negative value, the calculations may not make sense in the context of time duration, +// as negative time durations are not typically represented in hours, minutes, and seconds. +// Additionally, if movieLength is not an integer (e.g., a floating-point number), the calculations may yield unexpected results due to the way division and modulus operations work with non-integer values. +// Therefore, it is advisable to ensure that movieLength is a non-negative integer for the code to function as intended. +// If movieLength is extremely large, the code will still work, but we should consider adding additional formatting to handle cases where the number of hours exceeds 24. diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..4f152cdd2 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,19 +1,22 @@ -const penceString = "399p"; +const penceString = "18399p"; const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 ); - +console.log(penceStringWithoutTrailingP); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 ); +console.log(pounds); + const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); +console.log(pence); console.log(`£${pounds}.${pence}`); @@ -25,3 +28,19 @@ 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); +// - This line removes the trailing 'p' from the pence string to isolate the numeric part +// - It uses the substring method to extract all characters from the start of the string up to (but not including) the last character +// 3. console.log(penceStringWithoutTrailingP): logs the modified pence string without the trailing 'p' to the console for verification +// 4. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// - This line ensures that the numeric string has at least three characters by padding it with leading zeros if necessary +// - This is important for correctly separating pounds and pence later on +// 5. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); +// - This line extracts the pounds part of the price by taking all characters except for the last two (which represent pence) +// 6. console.log(pounds): logs the extracted pounds value to the console for verification +// 7. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +// - This line extracts the pence part of the price by taking the last two characters of the padded string +// - It also ensures that there are always two digits for pence by padding with a trailing zero if necessary +// 8. console.log(pence): logs the extracted pence value to the console for verification +// 9. console.log(`£${pounds}.${pence}`); +// - Finally, this line constructs and logs the final price in pounds and pence format, prefixed with the pound symbol (£) diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..1c1f32d74 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -13,4 +13,11 @@ Try also entering `typeof console` Answer the following questions: What does `console` store? +console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} +it provides a set of methods (functions) we can call to interact with the debugging console. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +log() { [native code] } +assert() { [native code] } +the log/assert methods inside console. +. - property accessor (like saying “go inside this object and grab this property or method”).