From 3809d8dbda614bed5e0fcdeef8ccd050275bba35 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Mon, 30 Jun 2025 03:29:05 +0100 Subject: [PATCH 01/19] declared a variable called initials that stores the frist characters of the names --- Sprint-1/1-key-exercises/1-count.js | 2 ++ Sprint-1/1-key-exercises/2-initials.js | 4 ++++ Sprint-2/1-key-errors/2.js | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..b10542af5 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 +// Line 3 is incrementing the count variable by 1, effectively adding 1 to its current value +// The = operator is used to assign the new value (count + 1) back to the count variable diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..7f56eee88 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -8,4 +8,8 @@ let lastName = "Johnson"; let initials = ``; // https://www.google.com/search?q=get+first+character+of+string+mdn +initials += firstName[0]; +initials += middleName[0]; +initials += lastName[0]; + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..37bc839c6 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,26 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// The code will throw an error because the function is trying to use a number literal (3) as a parameter name, +// which is not allowed in JavaScript. Function parameters must be valid identifiers, and a number -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } // =============> write the error message here +// SyntaxError: Unexpected number in parameter list // =============> explain this error message here +// The error message indicates that there is a syntax error because the function parameter cannot be a number. +// Function parameters must be valid identifiers, and using a number as a parameter name is not allowed // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(3)); // Output: 9 From 7fa9849fcf94ff9dfadd05b045ff3391952faea9 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Mon, 30 Jun 2025 03:34:12 +0100 Subject: [PATCH 02/19] the declared variable produce the string "CKJ". --- Sprint-1/1-key-exercises/2-initials.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 7f56eee88..15a000f58 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -11,5 +11,7 @@ let initials = ``; initials += firstName[0]; initials += middleName[0]; initials += lastName[0]; +console.log(initials); // Output: "CKJ" + From a5d88d4f8d5171763592b05202687577d0c3d7f1 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Mon, 30 Jun 2025 03:49:12 +0100 Subject: [PATCH 03/19] 1. got filename and the directory plus the file extention. --- Sprint-1/1-key-exercises/3-paths.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..c2b347ea2 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -9,15 +9,28 @@ // (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"; +// const lastSlashIndex = filePath.lastIndexOf("/"); +// 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 const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; + +// Get base (file name) const lastSlashIndex = filePath.lastIndexOf("/"); 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 +// Get dir (directory path) +const dir = filePath.slice(0, lastSlashIndex); -const dir = ; -const ext = ; +// Get ext (file extension) +const lastDotIndex = base.lastIndexOf("."); +const ext = base.slice(lastDotIndex); + +console.log(`The base part of ${filePath} is ${base}`); +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 2dcd035e444a9fe2bdc7c4bc665b9a080da91f6e Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Mon, 30 Jun 2025 04:00:12 +0100 Subject: [PATCH 04/19] 1. worked out what num represents, break down the expression. 2. logged the value of num and run it. --- Sprint-1/1-key-exercises/4-random.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..2974a3315 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(`The random number is: ${num}`); +// The expression Math.random() generates a random floating-point number between 0 (inclusive) and 1 (exclusive). +// Multiplying this by (maximum - minimum + 1) scales the range to the desired range of numbers. +// Adding minimum shifts the range to start from the minimum value. +// Finally, Math.floor() rounds down the result to the nearest whole number, ensuring that num is an integer within the specified range. +// The final result is a random integer between minimum and maximum, inclusive. +// In this case, num will be a random integer between 1 and 100, inclusive. +// The program will output a different random number each time it is run, within the specified range + From 90ed1946a976020f14d1af23cffa8c439f3f3d29 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Mon, 30 Jun 2025 04:03:06 +0100 Subject: [PATCH 05/19] 1. turn the two lines into comments --- Sprint-1/2-mandatory-errors/0.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..4a9a46fd7 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,3 @@ -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? +// You can use a comment to prevent the computer from running these lines \ No newline at end of file From f62310430675d920a50e4c39ec537d761e1183d8 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Wed, 2 Jul 2025 01:41:06 +0100 Subject: [PATCH 06/19] 1. interpreted the age variable declaration as const age 2. fixed the error by changing the declaration of age to 'let', and reassignment. --- Sprint-1/1-key-exercises/1-count.js | 2 ++ Sprint-1/2-mandatory-errors/1.js | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index b10542af5..01ba5b15f 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -6,3 +6,5 @@ count = count + 1; // Describe what line 3 is doing, in particular focus on what = is doing // Line 3 is incrementing the count variable by 1, effectively adding 1 to its current value // The = operator is used to assign the new value (count + 1) back to the count variable +// The code is valid and will not throw an error +// because count is declared with let, allowing it to be reassigned. diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..ea5b39b1f 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,15 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; -age = age + 1; +// const age = 33; +// age = age + 1; +// The error occurs because the variable 'age' is declared as a constant using 'const'. +// Constants cannot be reassigned a new value after they are declared. +// To fix this error, you can change the declaration of 'age' to 'let' or 'var' if you want to reassign it later. +// For example: let + age = 33; + age = age + 1; +// Now, 'age' can be reassigned without any error. +// Alternatively, if you want to keep 'age' as a constant, you should not attempt to reassign it. +// In that case, you can simply remove the reassignment line: +console.log(age); // Output: 34 +// This will output the value of 'age' after the reassignment, which is now 34. From c71036a92c02a3e91d9d14ee63eda9d6d75d77e4 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Wed, 2 Jul 2025 01:52:33 +0100 Subject: [PATCH 07/19] 1.the error is the use of the variable 'cityOfBirth' before it's declared. 2. to fix it , I declared the variable before using it in console.log --- Sprint-1/2-mandatory-errors/2.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..19f37bdfa 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 occurs because the variable 'cityOfBirth' is used before it is declared. +// In JavaScript, variables declared with 'const' or 'let' are not hoisted to the top of their scope. +// To fix this error, you should declare the variable 'cityOfBirth' before using it in the console.log statement. const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); +// This will correctly output "I was born in Bolton" without any errors. From 31f048943aa578eb8af9c2553132ca2c918b665d Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Wed, 2 Jul 2025 02:09:06 +0100 Subject: [PATCH 08/19] 1. The error occurs because the `slice` method is being called on a number while it's a string. 2. To fix this error, we need to convert the `cardNumber` to a string before using the `slice` method. --- Sprint-1/2-mandatory-errors/3.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..27b374879 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,5 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +// const cardNumber = 4533787178994213; +// const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,15 @@ 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 + +// console.log(`The last 4 digits of the card number are: ${last4Digits}`); + +// The error occurs because the `slice` method is being called on a number, which is not a valid operation. +// The `slice` method is a string method, and it cannot be directly applied to a number. +// To fix this error, we need to convert the `cardNumber` to a string before using the `slice` method. +// We can do this by wrapping `cardNumber` in the `String()` function or using the `toString()` method. +// Here's the corrected code: +const cardNumber = 4533787178994213; +const last4Digits = String(cardNumber).slice(-4); +console.log(`The last 4 digits of the card number are: ${last4Digits}`); +// Now, the code will correctly output the last 4 digits of the card number, which are "4213". From 1156c039f852cb962e5600052a7ef311e3f67c67 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Wed, 2 Jul 2025 02:32:24 +0100 Subject: [PATCH 09/19] 1. The error occurs because the variable name '12HourClockTime' starts with a digit. 2. To fix this error, I renamed the variable to start with a letter or an underscore. --- Sprint-1/2-mandatory-errors/4.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..bfc1469a6 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,13 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; +// const 24hourClockTime = "08:53"; + +// The error occurs because the variable name '12HourClockTime' starts with a digit, which is not allowed in JavaScript. +// Variable names must begin with a letter, underscore (_), or dollar sign ($). +// To fix this error, we can rename the variable to start with a letter or an underscore. +// For example, we can rename it to 'hour12ClockTime' or '_12HourClockTime'. +const hour12ClockTime = "20:53"; +const hour24ClockTime = "08:53"; + +console.log(`The 12-hour clock time is: ${hour12ClockTime}`); +console.log(`The 24-hour clock time is: ${hour24ClockTime}`); +// This will correctly output the 12-hour and 24-hour clock times without any errors. From 40d86f14bf7b9b07e750019e15af9b34ec1cfef7 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Wed, 2 Jul 2025 03:01:55 +0100 Subject: [PATCH 10/19] 1. Wrote down the 3 lines where a function call is made. 2. Ran the code and identified the line 7 where the error is coming from . 3. Identified all the lines that are variable reassignment statements. 4. Identified all the 5 lines that are variable declarations. 5. Described what the expression Number(carPrice.replaceAll(",","")) is doing. 6. corrected the code. --- .../1-percentage-change.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..bbfbd04dc 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,11 +12,35 @@ 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 3 function calls in this file: +// 1. `carPrice.replaceAll(",", "")` on line 6 +// 2. `priceAfterOneYear.replaceAll("," "")` on line 7 +// 3. `console.log(...)` on line 12 // 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 occurs on line 7: `priceAfterOneYear.replaceAll("," "")` +// The error is due to a syntax mistake in the `replaceAll` method call; there is an extra space between the comma and the closing double quote. + // c) Identify all the lines that are variable reassignment statements +// The variable reassignment statements are: +// 1. `carPrice = Number(carPrice.replaceAll(",", ""));` on line 6 +// 2. `priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));` on line 7 // d) Identify all the lines that are variable declarations +// The variable declarations are: +// 1. `let carPrice = "10,000";` on line 1 +// 2. `let priceAfterOneYear = "8,543";` on line 2 +// 3. `const priceDifference = carPrice - priceAfterOneYear;` on line 9 +// 4. `const percentageChange = (priceDifference / carPrice) * 100;` on line 10 +// 5. `console.log(...)` on line 12 (though this is a function call, it also serves as a declaration in this context) // 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 representation of the car price (which includes commas) into a number. +// It first removes all commas from the string using `replaceAll(",", "")`, and then converts the resulting string into a number using the `Number` function. +// This is necessary to perform arithmetic operations on the car price, as arithmetic operations cannot be performed directly on strings. + +//correct the code +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); +// The corrected line should be: \ No newline at end of file From b5fd505aba7a28452b6b4eea24b20bb763099166 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Wed, 2 Jul 2025 03:03:15 +0100 Subject: [PATCH 11/19] saved the changes --- .../3-mandatory-interpret/1-percentage-change.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index bbfbd04dc..921230b2b 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -1,13 +1,13 @@ -let carPrice = "10,000"; -let priceAfterOneYear = "8,543"; +// let carPrice = "10,000"; +// let priceAfterOneYear = "8,543"; -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +// carPrice = Number(carPrice.replaceAll(",", "")); +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); -const priceDifference = carPrice - priceAfterOneYear; -const percentageChange = (priceDifference / carPrice) * 100; +// const priceDifference = carPrice - priceAfterOneYear; +// const percentageChange = (priceDifference / carPrice) * 100; -console.log(`The percentage change is ${percentageChange}`); +// console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below From 0c90651b8a6b99ad7efbc76505f571724b7f5569 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Wed, 2 Jul 2025 03:40:22 +0100 Subject: [PATCH 12/19] 1. answered all the questions about code. --- .../3-mandatory-interpret/1-percentage-change.js | 13 +++++++++++++ Sprint-1/3-mandatory-interpret/2-time-format.js | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index 921230b2b..4ba490410 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -41,6 +41,19 @@ // This is necessary to perform arithmetic operations on the car price, as arithmetic operations cannot be performed directly on strings. //correct the code +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; + +// Remove commas and convert to numbers carPrice = Number(carPrice.replaceAll(",", "")); priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + +// Calculate difference and percentage change +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; + +// Output the result +console.log(`The percentage change is ${percentageChange.toFixed(2)}%`); + + // The corrected line should be: \ 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..8ecd041cf 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,25 @@ 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: +// 1. `movieLength` on line 1 +// 2. `remainingSeconds` on line 3 +// 3. `totalMinutes` on line 4 +// 4. `remainingMinutes` on line 6 +// 5. `totalHours` on line 7 // b) How many function calls are there? +// There are no function calls in this program. All operations are performed using arithmetic operators and string interpolation. // 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` calculates the remainder when `movieLength` (in seconds) is divided by 60. This gives the number of seconds that do not fit into complete minutes, effectively providing the remaining seconds after converting to minutes. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The expression `(movieLength - remainingSeconds) / 60` calculates the total number of minutes in the movie. It first subtracts the remaining seconds from the total movie length (to account for any leftover seconds), and then divides the result by 60 to convert seconds into minutes. // 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 time of the movie in the format "hours:minutes:seconds". A better name for this variable could be `formattedMovieTime` or `movieDuration` to make it clearer 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 +// Yes, this code will work for all non-negative integer values of `movieLength`. It will correctly convert any length of the movie given in seconds into the format "hours:minutes:seconds". However, if `movieLength` is negative, the output will not be meaningful, as negative time does not have a valid representation in this context. Therefore, it is advisable to ensure that `movieLength` is a non-negative integer before running the code. From b2e98ebfa2903adad1c3b918fd1d10f346db928a Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Thu, 3 Jul 2025 03:19:16 +0100 Subject: [PATCH 13/19] interpreted the code and tested --- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..a85d8315e 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,4 +1,4 @@ -const penceString = "399p"; +const penceString = "399"; const penceStringWithoutTrailingP = penceString.substring( 0, @@ -22,6 +22,12 @@ console.log(`£${pounds}.${pence}`); // You need to do a step-by-step breakdown of each line in this program // Try and describe the purpose / rationale behind each step +// The purpose of this program is to convert a string representing a price in pence (e.g., "399p") into a formatted string representing the price in pounds (e.g., "£3.99"). +// The program achieves this by manipulating the string to extract the numeric value and format it correctly. // 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 string, resulting in "399". +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the string with leading zeros to ensure it has at least 3 characters, resulting in "399" (no change needed here since it already has 3 characters). +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds part of the string by taking all characters except the last two, resulting in "3". +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumber From 50cfabe03506ae5f64e1f26acc817f537786431a Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Thu, 3 Jul 2025 19:18:27 +0100 Subject: [PATCH 14/19] 1. used `console.log` to check the value of variable --- Sprint-1/1-key-exercises/1-count.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 01ba5b15f..a734038ac 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -8,3 +8,4 @@ count = count + 1; // The = operator is used to assign the new value (count + 1) back to the count variable // The code is valid and will not throw an error // because count is declared with let, allowing it to be reassigned. +console.log(count); // Output: 1 From 6629ecdd0b8e6d4aca5307f1170b0131b00d0568 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sun, 6 Jul 2025 22:14:42 +0100 Subject: [PATCH 15/19] fixed the reason for the error occurs on line 7. --- Sprint-1/3-mandatory-interpret/1-percentage-change.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index 4ba490410..24607ee23 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -19,8 +19,8 @@ // 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 occurs on line 7: `priceAfterOneYear.replaceAll("," "")` -// The error is due to a syntax mistake in the `replaceAll` method call; there is an extra space between the comma and the closing double quote. - +// The error is due to a syntax mistake in the `replaceAll` method call. There is a missing comma between the two arguments. +// To fix this, we need to add the missing comma, it should be `priceAfterOneYear.replaceAll(",", "")`. // c) Identify all the lines that are variable reassignment statements // The variable reassignment statements are: @@ -55,5 +55,4 @@ const percentageChange = (priceDifference / carPrice) * 100; // Output the result console.log(`The percentage change is ${percentageChange.toFixed(2)}%`); - // The corrected line should be: \ No newline at end of file From 6378e92c25025d63489171684bc6c25be51f5a34 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sun, 6 Jul 2025 22:45:17 +0100 Subject: [PATCH 16/19] identified the number of the function calls in total. --- Sprint-1/3-mandatory-interpret/1-percentage-change.js | 10 ++++++---- 1 file changed, 6 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 24607ee23..667be3cb7 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,10 +12,12 @@ // 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 3 function calls in this file: -// 1. `carPrice.replaceAll(",", "")` on line 6 -// 2. `priceAfterOneYear.replaceAll("," "")` on line 7 -// 3. `console.log(...)` on line 12 +//There are 5 function calls in total. +// 1. `carPrice.replaceAll(",", "")` on line 4. +// 2. numbeer(carPrice.replaceAll(",", ""))` on line 4. +// 3. `priceAfterOneYear.replaceAll("," "")` on line 5. +// 4. `Number(priceAfterOneYear.replaceAll("," ""))` on line 5. +// 5. `console.log(...)` on line 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? // The error occurs on line 7: `priceAfterOneYear.replaceAll("," "")` From 6cf4e18743c483e721a615d5c6bcc1f48a59336f Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sun, 6 Jul 2025 22:50:01 +0100 Subject: [PATCH 17/19] reverted the changes done to a file in Sprint-2. --- Sprint-2/1-key-errors/2.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index 37bc839c6..aad57f7cf 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,26 +4,17 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here -// The code will throw an error because the function is trying to use a number literal (3) as a parameter name, -// which is not allowed in JavaScript. Function parameters must be valid identifiers, and a number -// function square(3) { -// return num * num; -// } +function square(3) { + return num * num; +} // =============> write the error message here -// SyntaxError: Unexpected number in parameter list // =============> explain this error message here -// The error message indicates that there is a syntax error because the function parameter cannot be a number. -// Function parameters must be valid identifiers, and using a number as a parameter name is not allowed // Finally, correct the code to fix the problem // =============> write your new code here -function square(num) { - return num * num; -} -console.log(square(3)); // Output: 9 From 88084e2b8bc0e856bd53dbda1ccdf58036ce3a66 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sun, 6 Jul 2025 23:23:44 +0100 Subject: [PATCH 18/19] fixed the line numbering in my answers --- Sprint-1/3-mandatory-interpret/1-percentage-change.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index 667be3cb7..82b54a8dc 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -26,16 +26,16 @@ // c) Identify all the lines that are variable reassignment statements // The variable reassignment statements are: -// 1. `carPrice = Number(carPrice.replaceAll(",", ""));` on line 6 -// 2. `priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));` on line 7 +// 1. `carPrice = Number(carPrice.replaceAll(",", ""));` on line 4 +// 2. `priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));` on line 5 // d) Identify all the lines that are variable declarations // The variable declarations are: // 1. `let carPrice = "10,000";` on line 1 // 2. `let priceAfterOneYear = "8,543";` on line 2 -// 3. `const priceDifference = carPrice - priceAfterOneYear;` on line 9 -// 4. `const percentageChange = (priceDifference / carPrice) * 100;` on line 10 -// 5. `console.log(...)` on line 12 (though this is a function call, it also serves as a declaration in this context) +// 3. `const priceDifference = carPrice - priceAfterOneYear;` on line 7 +// 4. `const percentageChange = (priceDifference / carPrice) * 100;` on line 8 +// 5. `console.log(...)` on line 10 (though this is a function call, it also serves as a declaration in this context) // 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 representation of the car price (which includes commas) into a number. From 40faf4abe10e6ad9b6ae9ecc0f19bee320653a20 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sun, 6 Jul 2025 23:50:43 +0100 Subject: [PATCH 19/19] tested the code after removing .padEnd(2, "0");. Also removed unneccary spaces. --- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index a85d8315e..47d6b0c07 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,20 +1,13 @@ const penceString = "399"; -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); +const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); +const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2); const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); - console.log(`£${pounds}.${pence}`); // This program takes a string representing a price in pence