diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..d73658664 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 reassigning a new value to the 'count' variable. +// In JavaScript, '=' is the assignment operator, not a mathematical equals sign. +// It first evaluates the expression on the right side (count + 1, which is 0 + 1), +// and then assigns that result (1) back to the variable on the left side, updating its value. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..0a1e1535c 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]}`; +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..73ba859dc 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,8 @@ 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(".")); +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..a979d4e11 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 +console.log(num); +// Answer: The variable `num` represents a random whole number between 1 and 100 (inclusive). +// Breakdown of the expression: +// 1. Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). +// 2. (maximum - minimum + 1) calculates the total number of possible integers in our range (which is 100). +// 3. Multiplying them scales the random decimal to be between 0 and 99.999... +// 4. Math.floor() rounds that decimal down to the nearest whole number (getting an integer from 0 to 99). +// 5. Finally, '+ minimum' shifts the starting point up by 1, resulting in a final integer from 1 to 100. \ 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..b6fb6d9cb 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,7 @@ -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? + +// Explanation: +// Running this without comments gives a "SyntaxError". +// This occurs because the computer tries to read plain English sentences as JavaScript code, which it cannot understand. +// Adding '//' turns them into comments that the computer ignores. \ 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..5a282f8fd 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,8 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; + +// Explanation: +// The error is "TypeError: Assignment to constant variable." +// This occurs because we used 'const' to declare the 'age' variable. 'const' creates a constant whose value cannot be reassigned after its initial creation. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..16cf15164 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,8 @@ // 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}`); + +// Explanation: +// The error is "ReferenceError: Cannot access 'cityOfBirth' before initialization." +// JavaScript executes code from top to bottom. We tried to print 'cityOfBirth' before we actually declared and initialized it on the next line. \ 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..fb7e57d47 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,13 @@ -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 // 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 + +// Explanation: +// The error is "TypeError: cardNumber.slice is not a function." +// This occurs because the original cardNumber was a Number type. The '.slice()' method only works on Strings and Arrays, not on pure numbers. \ 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..393b54f35 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,9 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const time12Hour = "20:53"; +const time24Hour = "08:53"; + +console.log(time12Hour); +console.log(time24Hour); + +// Explanation: +// The error is a "SyntaxError". +// This occurs because it violates JavaScript naming conventions: variable names cannot start with a number. \ 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..fa2842603 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,11 +12,12 @@ 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 - +// Answer: There are 5 function calls in total. They are located on lines 4, 5, and 10. // 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? - +// Answer: The error comes from line 4. Since JavaScript executes from top to bottom, it crashes on line 4 because we are trying to reassign a new value to `carPrice`, which was originally declared as a `const`. To fix it, change `const` to `let` on lines 1 and 2. // c) Identify all the lines that are variable reassignment statements - +// Answer: Lines 4 and 5. // d) Identify all the lines that are variable declarations - +// Answer: Lines 1, 2, 7, and 8. // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Answer: It removes the comma from the string using `replaceAll()`, and then converts that cleaned string into a number type using `Number()`. The purpose is to convert a formatted text string into a valid mathematical number so we can do calculations with it later. \ 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..ee3e57f7a 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,15 @@ 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? - +// Answer: There are 6 variable declarations. (They are: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result). // b) How many function calls are there? - +// Answer: There is exactly 1 function call (console.log on line 10). // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators - +// Answer: The `%` symbol is the remainder (modulo) operator. `movieLength % 60` calculates the leftover seconds that do not fit into a full 60-second minute. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - +// Answer: It subtracts the leftover seconds from the total seconds to get a number cleanly divisible by 60, then divides by 60 to find out exactly how many full minutes are in the movie. // e) What do you think the variable result represents? Can you think of a better name for this variable? - +// Answer: `result` represents the final formatted time string (Hours:Minutes:Seconds). A better, more descriptive name could be `formattedTime` or `movieDurationString`. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Answer: It works for positive whole numbers. However, it won't format single-digit numbers perfectly (e.g., it prints 2:5:9 instead of 02:05:09). Also, it might produce weird results or decimals if we input negative numbers or fractional 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..f9380dbc3 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,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 = ... +// Removes the last character (the "p") from the string using substring(), leaving just the numbers. + +// 3. const paddedPenceNumberString = ... +// Uses padStart(3, "0") to make sure the string is at least 3 characters long by adding "0"s to the front. + +// 4. const pounds = ... +// Uses substring() to extract all characters EXCEPT the last two. This isolates the pound value. + +// 5. const pence = ... +// Extracts exactly the last two characters for the pence value using substring(), +// and uses padEnd() as a safety measure to ensure it's exactly 2 digits long. + +// 6. console.log(...) +// Uses template literals to piece the pounds and pence back together with a "£" and "." symbol, printing "£3.99". \ No newline at end of file