From 07c5cbe1a9f9d18029d4d2a7aa3005e99cb061d1 Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 14 Oct 2025 20:23:20 +0100 Subject: [PATCH 01/16] Add comments to clarify variable declaration and assignment in count.js --- Sprint-1/1-key-exercises/1-count.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..a3a56e4b7 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 is used for assignment, meaning it takes the result of the expression on the right (count + 1) and assigns that new value back to the count variable. \ No newline at end of file From 7bca1d21b569a3dae38146dfc2da9ae8f73a12c0 Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 14 Oct 2025 20:49:47 +0100 Subject: [PATCH 02/16] Implement initials extraction and log output in 2-initials.js --- Sprint-1/1-key-exercises/2-initials.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..0348e5553 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,9 @@ 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); +// Should print "CKJ" \ No newline at end of file From 2b57ffd11aa3adeea83e5adbb2319fcf3f549042 Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 14 Oct 2025 21:12:09 +0100 Subject: [PATCH 03/16] Fix variable assignments for dir and ext in 3-paths.js --- Sprint-1/1-key-exercises/3-paths.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..027e48890 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 = filePath.slice(lastSlashIndex); + +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 From 76e3f65ae46d3b17f928f58cc5b804005bcd1fa1 Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 14 Oct 2025 21:19:24 +0100 Subject: [PATCH 04/16] Enhance comments to clarify the random number generation process in 4-random.js --- Sprint-1/1-key-exercises/4-random.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..b6b107d9a 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -4,6 +4,31 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // In this exercise, you will need to work out what num represents? + +//After running this exercise a few times I can see that 'num' is a random number between 1 and 100 + +console.log(num); + // Try breaking down the expression and using documentation to explain what it means + +// at the top we can see our minimum and maximum values which are fixed therefore Constants, +// we then have our variable num which is assigned a value using the Math object and its methods floor and random +// Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive) +// We then multiply this random number by the range of our desired numbers which is (maximum - minimum + 1) + // It will help to think about the order in which expressions are evaluated + +//the same as math, multiplication is done before addition and subtraction +// so we first calculate (maximum - minimum + 1) which is (100 - 1 + 1) = 100 +// then we multiply the random decimal number by 100 which gives us a number between 0 and 100 (but not including 100) +// then we apply Math.floor() to round down to the nearest whole number, giving us a number between 0 and 99 +// finally we add the minimum value (1) to shift the range up, resulting in a final value between 1 and 100 (inclusive) + +// So in summary, 'num' is a random integer between 1 and 100, inclusive of both endpoints. + + // Try logging the value of num and running the program several times to build an idea of what the program is doing + +//you can see how random function generates different numbers each time the program is run +//and how the range is always between 1 and 100 + From afca59a1bbb772636c61faefb3c91092f953ba71 Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 14 Oct 2025 21:34:58 +0100 Subject: [PATCH 05/16] Refactor comments for clarity and fix variable naming conflict in capitalise function --- Sprint-1/2-mandatory-errors/0.js | 6 ++++-- Sprint-2/1-key-errors/0.js | 27 +++++++++++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..74ae767bc 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? + +// in code use backslashes to create comments. \ No newline at end of file diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..ba0a56cc2 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,28 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: +// I think their will be an error because of the str tag + + + + // call the function capitalise with a string input +console.log(capitalise("hello")); // should print "Hello" + + // interpret the error message and figure out why an error is occurring -function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; -} -// =============> write your explanation here +//function capitalise(str) { + //let str = `${str[0].toUpperCase()}${str.slice(1)}`; + //return str; +//} + +// =============> write your explanation here: +// str is being used as both the function parameter and a variable inside the function, causing a conflict. + + // =============> write your new code here + +function capitalise(inputStr) { + return `${inputStr[0].toUpperCase()}${inputStr.slice(1)}`;} From 2b12a3404ee02ea40506df2dc28be306c5ce4c3f Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 14 Oct 2025 21:39:29 +0100 Subject: [PATCH 06/16] Change age from constant to variable for reassignment and update comments for clarity --- Sprint-1/2-mandatory-errors/1.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..0f926e8f3 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; // changed from constant to variable so that value can be reassigned age = age + 1; +console.log(age); // should print 34 running testing + + +//after testing can confirm age variable is now 34. \ No newline at end of file From b7a8c22227a6366044bb4fcf9e77e392d398f4d4 Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 14 Oct 2025 21:45:59 +0100 Subject: [PATCH 07/16] Fix variable hoisting issue by moving cityOfBirth declaration above console.log and enhance comments for clarity (2.js) --- Sprint-1/2-mandatory-errors/2.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..6846daf7c 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,6 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + + +// Code is executed line by line so the variable cityOfBirth is not defined when it is called in the console.log line. \ No newline at end of file From 993c40c50883f4dbb3788f77f3d6d3ca3981476a Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 18 Oct 2025 18:55:14 +0100 Subject: [PATCH 08/16] Fix last4Digits assignment by converting cardNumber to string and update comments for clarity --- Sprint-1/2-mandatory-errors/3.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..6876d6074 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,18 @@ 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 +// CardNumber is not a funtion so it does not have the method slice. + // Then run the code and see what error it gives. +// TypeError: cardNumber.slice is not a function // Consider: Why does it give this error? Is this what I predicted? If not, what's different? +//this is what I predicted because slice is a method for strings and arrays, not numbers. // Then try updating the expression last4Digits is assigned to, in order to get the correct value + + +const last4DigitsCorrected = cardNumber.toString().slice(-4); + +console.log(last4DigitsCorrected); // Should output: 4213 \ No newline at end of file From d8109f2141596ecfe0ee8701540970c32f02f156 Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 18 Oct 2025 18:59:38 +0100 Subject: [PATCH 09/16] Rename variables to avoid starting with numbers and update comments for clarity 4.js --- Sprint-1/2-mandatory-errors/4.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..6f53537d9 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const Twelve_HourClockTime = "20:53"; +const Twenty_Four_hourClockTime = "08:53"; + +//Variables cannot have numbers at the start of their name +//the times are in the wrong format for their variable names \ No newline at end of file From 13c32aaff6f65b1a11da28a3d09956450a65d7ba Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 18 Oct 2025 19:04:44 +0100 Subject: [PATCH 10/16] Fix syntax error in priceAfterOneYear assignment and enhance comments for clarity --- Sprint-1/3-mandatory-interpret/1-percentage-change.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..01cbe4d49 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,21 +2,24 @@ 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; 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: carPrice.replaceAll(",", ""), Number(carPrice.replaceAll(",", "")), priceAfterOneYear.replaceAll(",", ""), Number(priceAfterOneYear.replaceAll(",", "")), and 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 comes from the line priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); because there is a missing comma between the arguments in replaceAll, which causes a syntax error. It can be fixed by writing replaceAll(",", "") instead. // c) Identify all the lines that are variable reassignment statements +// The variable reassignment statements are carPrice = Number(carPrice.replaceAll(",", "")); and priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));. // d) Identify all the lines that are variable declarations +// The variable declarations are let carPrice = "10,000";, let priceAfterOneYear = "8,543";, const priceDifference = carPrice - priceAfterOneYear;, and const percentageChange = (priceDifference / carPrice) * 100;. -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing - what is the purpose of this expression? +// The expression first removes the comma from the string "10,000" using replaceAll(",", ""), resulting in "10000", and then converts that string into a number using Number(), so that it can be used in mathematical calculations. From 5f6ff93fa7d747a509a04a08d048292120737c15 Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 18 Oct 2025 19:06:14 +0100 Subject: [PATCH 11/16] Enhance comments for clarity and remove unnecessary lines in time format calculation 2-time-format.js --- Sprint-1/3-mandatory-interpret/2-time-format.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..c141cf2e7 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -9,17 +9,20 @@ const totalHours = (totalMinutes - remainingMinutes) / 60; 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 six variable declarations: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result. // b) How many function calls are there? +// There is one function call in this program: 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 uses the modulo (%) operator to find the remainder after dividing movieLength by 60. This gives the number of seconds left over after converting all full minutes from the total seconds. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The expression (movieLength - remainingSeconds) / 60 converts the total number of seconds (minus any leftover seconds) into full minutes, effectively giving the total number of complete minutes in the movie. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result represents the movie length converted from seconds into hours, minutes, and seconds in the format "hours:minutes:seconds". A better name for this variable would be formattedDuration or timeString for clarity. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// This code works correctly for positive integer values of movieLength because it cleanly divides seconds into hours, minutes, and seconds. However, it will not work properly for negative numbers or non-integer values, and it doesn’t add leading zeros (e.g. it might print 2:5:3 instead of 2:05:03). From f4d3f59a408fc2576971cea5c0c131ba94896260 Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 18 Oct 2025 19:07:53 +0100 Subject: [PATCH 12/16] Refactor comments for clarity and remove redundant explanations in 3-to-pounds.js --- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..63998bff3 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -17,11 +17,9 @@ const pence = paddedPenceNumberString console.log(`£${pounds}.${pence}`); -// This program takes a string representing a price in pence -// The program then builds up a string representing the price in pounds - -// You need to do a step-by-step breakdown of each line in this program -// 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 by taking all characters from index 0 up to but not including the last one, leaving "399". This isolates the numeric part of the price. +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): ensures the numeric string has at least three digits by adding leading zeros if needed (for example, "5p" becomes "005"). This makes it easier to separate pounds and pence later. +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts all digits except the last two to represent the pounds. For "399" this gives "3". +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): takes the last two digits of the string as pence, and if needed, pads the end with a zero to ensure two digits. For "399" this gives "99". +// 6. console.log(`£${pounds}.${pence}`): prints the formatted price as a string in the style of "£3.99". The program as a whole converts a pence-based string like "399p" into a properly formatted pounds and pence display. From a33085b30964b7aa76c3e1be3418de248381efdb Mon Sep 17 00:00:00 2001 From: Ali Date: Sun, 16 Nov 2025 14:59:43 +0000 Subject: [PATCH 13/16] Restore original file contents --- Sprint-2/1-key-errors/0.js | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index ba0a56cc2..653d6f5a0 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,28 +1,13 @@ // Predict and explain first... -// =============> write your prediction here: -// I think their will be an error because of the str tag - - - - +// =============> write your prediction here // call the function capitalise with a string input -console.log(capitalise("hello")); // should print "Hello" - - // interpret the error message and figure out why an error is occurring +function capitalise(str) { + let str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} -//function capitalise(str) { - //let str = `${str[0].toUpperCase()}${str.slice(1)}`; - //return str; -//} - -// =============> write your explanation here: -// str is being used as both the function parameter and a variable inside the function, causing a conflict. - - +// =============> write your explanation here // =============> write your new code here - -function capitalise(inputStr) { - return `${inputStr[0].toUpperCase()}${inputStr.slice(1)}`;} From b401592b6f34885b8fae519164f6aa26b30b7320 Mon Sep 17 00:00:00 2001 From: Ali Date: Sun, 16 Nov 2025 15:03:44 +0000 Subject: [PATCH 14/16] Rename variables to follow camelCase convention in 4.js --- Sprint-1/2-mandatory-errors/4.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 6f53537d9..53ede0aa0 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,5 +1,6 @@ -const Twelve_HourClockTime = "20:53"; -const Twenty_Four_hourClockTime = "08:53"; +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; + //Variables cannot have numbers at the start of their name //the times are in the wrong format for their variable names \ No newline at end of file From 6106da7630fd31e2fe79aaa9ad42342017171cf4 Mon Sep 17 00:00:00 2001 From: Ali Date: Sun, 16 Nov 2025 15:08:20 +0000 Subject: [PATCH 15/16] Refactor random number generation to use constants MIN and MAX, and enhance comments for clarity. --- Sprint-1/1-key-exercises/4-random.js | 39 ++++++---------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index b6b107d9a..7a8572e1c 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -1,34 +1,11 @@ -const minimum = 1; -const maximum = 100; +// MIN and MAX are constants that define the lower and upper bounds for our random number range. +const MIN = 1; +const MAX = 10; -const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; - -// In this exercise, you will need to work out what num represents? - -//After running this exercise a few times I can see that 'num' is a random number between 1 and 100 +// Math.random() returns a decimal value in the interval [0, 1) +// Multiplying by (MAX - MIN + 1) scales the interval to [0, MAX - MIN + 1) +// Applying Math.floor() converts the value to an integer within [s0, MAX - MIN] +// Finally, adding MIN shifts the interval to [MIN, MAX], which is our desired result. +const num = MIN + Math.floor(Math.random() * (MAX - MIN + 1)); console.log(num); - -// Try breaking down the expression and using documentation to explain what it means - -// at the top we can see our minimum and maximum values which are fixed therefore Constants, -// we then have our variable num which is assigned a value using the Math object and its methods floor and random -// Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive) -// We then multiply this random number by the range of our desired numbers which is (maximum - minimum + 1) - -// It will help to think about the order in which expressions are evaluated - -//the same as math, multiplication is done before addition and subtraction -// so we first calculate (maximum - minimum + 1) which is (100 - 1 + 1) = 100 -// then we multiply the random decimal number by 100 which gives us a number between 0 and 100 (but not including 100) -// then we apply Math.floor() to round down to the nearest whole number, giving us a number between 0 and 99 -// finally we add the minimum value (1) to shift the range up, resulting in a final value between 1 and 100 (inclusive) - -// So in summary, 'num' is a random integer between 1 and 100, inclusive of both endpoints. - - -// Try logging the value of num and running the program several times to build an idea of what the program is doing - -//you can see how random function generates different numbers each time the program is run -//and how the range is always between 1 and 100 - From e2f242e2aff3561e91c7240754c4b3093dad7fd1 Mon Sep 17 00:00:00 2001 From: Ali Date: Sun, 16 Nov 2025 15:13:14 +0000 Subject: [PATCH 16/16] Enhance comments for clarity and remove unnecessary lines in 3-paths.js --- Sprint-1/1-key-exercises/3-paths.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index 027e48890..870530a24 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -1,4 +1,4 @@ -// The diagram below shows the different names for parts of a file path on a Unix operating system +/// The diagram below shows the different names for parts of a file path on a Unix operating system // ┌─────────────────────┬────────────┐ // │ dir │ base │ @@ -7,20 +7,27 @@ // " / home/user/dir / file .txt " // └──────┴──────────────┴──────┴─────┘ -// (All spaces in the "" line should be ignored. They are purely for formatting.) - const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; + +// Find last slash to separate dir and base const lastSlashIndex = filePath.lastIndexOf("/"); + +// base = name + ext (e.g. "file.txt") const base = filePath.slice(lastSlashIndex + 1); 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 - +// dir = everything before the last slash const dir = filePath.slice(0, lastSlashIndex); -const ext = filePath.slice(lastSlashIndex); +// Find last dot to extract only extension +const lastDotIndex = filePath.lastIndexOf("."); + +// ext = everything from the last dot onward +const ext = filePath.slice(lastDotIndex); 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 + +// https://www.google.com/search?q=slice+mdn + +