diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..1d928616ce 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,7 @@ 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 an assignment operation. The right side evaluates first. + The assignment operator (=) then overwrites the old value with the new result. +*/ \ 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..46c3829920 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,8 @@ 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 ab90ebb28e..0608df1025 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,6 @@ 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(lastDotIndex); // 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..054dec138f 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,16 @@ 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 + +/* + What 'num' represents: A random whole integer between 1 and 100 inclusive. + + Order of evaluation breakdown: + 1. (maximum - minimum + 1) // Calculates total size of target range (100). + 2. Math.random() //Generates a random decimal between 0 and 0.999... + 3. Math.random() * 100 // Scales decimal up to a range between 0 and 99.999... + 4. Math.floor(...) // Rounds down to nearest integer, yielding 0 to 99. + 5. + minimum //Shifts final range up by 1, resulting in 1 to 100. +*/ + +console.log(num); diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea76..660e298389 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,5 @@ const age = 33; age = age + 1; + +// unlike let, const variables cannot be reassigned once created. \ 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 e09b89831d..d131fe180b 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,10 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +// ERROR: Cannot access 'cityOfBirth' before initialization console.log(`I was born in ${cityOfBirth}`); // ERROR: Cannot access 'cityOfBirth' before initialization +// Fixed: Swapped the line order to resolve the ReferenceError. + + +const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); // SUCCESS: Variable is initialized first diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 5f86c730bc..e10dc6f402 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const HourClockTime = "8:53pm"; +const hourClockTime = "20:53"; + +// Naming convention: In JavaScript, variable names cannot begin with a number. +// It would crash instantly with a syntax error: \ 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..bb6c74e101 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,8 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -20,3 +21,18 @@ console.log(`The percentage change is ${percentageChange}`); // d) Identify all the lines that are variable declarations // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +/* + a) There are two functions Line 4 & 5: + for eg, The expression Number(carPrice.replaceAll(",","")) is doing the following: + 1. carPrice.replaceAll(",","") - This removes any commas from the carPrice string. + 2. Number(...) - This converts the resulting string into a number. + b) The uncaught syntax error is coming from line 5. The error occurs because there is a missing comma in the replaceAll method. + It should be carPrice.replaceAll(",", "") instead of carPrice.replaceAll("," ""). To fix this, add the missing comma. +c) The variable reassignment statements are on lines 4 and 5, where carPrice and priceAfterOneYear are being reassigned to their numeric values. +d) The variable declarations are on lines 1 and 2, where carPrice and priceAfterOneYear are declared using the let keyword. + +e) The purpose of the expression Number(carPrice.replaceAll(",", "")) is to convert a string representation of a number (with commas as thousands separators) into an actual number that can be used in mathematical operations. + + Generally, the purpose of this expression is to convert a string representation of a number (with commas as thousands separators) into a actual number that can be used in mathematical operations. +*/ \ 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 47d2395587..4b9a89eace 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -23,3 +23,17 @@ console.log(result); // e) What do you think the variable result represents? Can you think of a better name for this variable? // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +/* +a) There are 6 variable declarations in this program: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result. + +b) There is 1 function call in this program: console.log(result). + +c) The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. This is used to determine the number of seconds that do not make up a full minute. + +d) The expression assigned to totalMinutes calculates the total number of minutes in the movie by subtracting the remaining seconds from the total length of the movie and then dividing by 60. This gives the whole number of minutes. +e) The variable result represents the formatted string of the movie length in hours, minutes, and seconds. A better name for this variable could be movieDuration. + +f) The code will work for all non-negative values of movieLength. However, if movieLength is negative, the calculations for remainingSeconds, totalMinutes, remainingMinutes, and totalHours may not produce meaningful results. +Additionally, if movieLength is not an integer, the calculations may also end up with unexpected results. +*/ \ 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..7ef3a3e472 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,8 @@ 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 removes the last character of the string (the "p"), leaving just the digits "399". +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): this pads the string with leading zeros until it is at least 3 characters long. Since "399" is already 3 characters, it stays "399". This step matters for smaller pence values, e.g. "9p" would become "009", ensuring there are always at least 2 digits left for pence. +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): this takes all characters except the last 2, giving the pounds part of the amount. For "399", this gives "3". +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): this takes the last 2 characters of the padded string (the pence part), then pads it with trailing zeros if it's shorter than 2 characters. For "399", this gives "99". +// 6. console.log(£${pounds}.${pence}): this combines the pounds and pence parts into a formatted price string, e.g. "£3.99".