From 37da673d1559a2c9de55b84d1490557dbd11d1fa Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Sun, 14 Sep 2025 15:11:27 +0100 Subject: [PATCH 1/4] sprint-1 exercies has been completed --- Sprint-1/1-key-exercises/2-initials.js | 3 ++- Sprint-1/1-key-exercises/3-paths.js | 8 ++++++-- Sprint-1/1-key-exercises/4-random.js | 10 +++++++++- Sprint-1/2-mandatory-errors/1.js | 3 ++- Sprint-1/2-mandatory-errors/2.js | 6 ++++-- Sprint-1/2-mandatory-errors/3.js | 9 ++++++--- Sprint-1/2-mandatory-errors/4.js | 9 +++++++-- Sprint-1/3-mandatory-interpret/1-percentage-change.js | 3 ++- 8 files changed, 38 insertions(+), 13 deletions(-) diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..07187d0d7 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.charAt(0)+middleName.charAt(0)+lastName.charAt(0)}`; +console.log(typeof(initials)) //==> CKJ // 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..dbae47e1a 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -14,10 +14,14 @@ 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 dir = ; -const ext = ; +const dir = filePath.slice(0,lastSlashIndex) + +const lastDoutIndex = base.lastIndexOf(".") +const ext = base.slice(lastDoutIndex) +console.log(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..052b6dcd1 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -1,9 +1,17 @@ const minimum = 1; const maximum = 100; -const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +const num = Math.floor(0.999 * (maximum - minimum + 1)) + minimum; // In this exercise, you will need to work out what num represents? // 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) +//num is a random number between the minimum and maximum. +// 1. Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1 +// 2. (maximum - minimum + 1) Calculates the range of values, including both ends. + // For 1 to 100, this is: + // 100 - 1 + 1 = 100 + //3- Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number. + // example Math.floor(50.9) = 50 \ 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..96fef328b 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +// const age = 33; +let age = 33; age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..5c8a9c7e7 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,7 @@ // 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"; +console.log(`I was born in ${cityOfBirth}`); +// Variables declared with (const) (or let) are not accessible before their declaration, even though they are hoisted (set aside in memory). +// // So, when console.log(...) runs, cityOfBirth exists in memory but is not yet initialized, so trying to access it throws an error. diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..443118aa0 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,12 @@ -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 // 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 +// .slice() called on a number, and Numbers don’t have string methods like .slice().it will throw an error +// console.log(last4Digits) => TypeError: cardNumber.slice is not a function at Object. +const cardNumber = 4533787178994213; +// const last4Digits = cardNumber.toString; +const last4Digits = cardNumber % 10000; +console.log(last4Digits) \ 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..d0063213e 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,7 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; +// const 24hourClockTime = "08:53"; +// JavaScript does not allow variable names to begin with a digit. The variable name 12HourClockTime starts with 12, +// which is not valid syntax, and will throw a SyntaxError: + +const hourClockTime24 = "20:53"; +const hourClockTime12 = "08:53"; \ 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..577112a74 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,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,6 +12,7 @@ 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 + // 3 functions in 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? From 73093978ab28c716e664388737aecd167702a02b Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Sun, 14 Sep 2025 18:47:22 +0100 Subject: [PATCH 2/4] sprint-1 exercies has been completed --- Sprint-1/1-key-exercises/1-count.js | 2 ++ Sprint-1/1-key-exercises/2-initials.js | 2 +- Sprint-1/1-key-exercises/3-paths.js | 1 - Sprint-1/1-key-exercises/4-random.js | 2 +- Sprint-1/2-mandatory-errors/0.js | 6 ++++-- Sprint-1/2-mandatory-errors/1.js | 1 + .../1-percentage-change.js | 11 +++++++++- .../3-mandatory-interpret/2-time-format.js | 11 +++++++++- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 21 +++++++++++++++++++ 9 files changed, 50 insertions(+), 7 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..359898770 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 +// is assignment it takes the variable vlue, which is 0 and add 1. +// // Since count was initially 0, this becomes: count = 0 + 1, so count now holds the value 1. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 07187d0d7..231f535b4 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -6,7 +6,7 @@ let lastName = "Johnson"; // 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 = `${firstName.charAt(0)+middleName.charAt(0)+lastName.charAt(0)}`; -console.log(typeof(initials)) //==> CKJ +// console.log(initials) ==> CKJ // 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 dbae47e1a..354af57dc 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -22,6 +22,5 @@ const dir = filePath.slice(0,lastSlashIndex) const lastDoutIndex = base.lastIndexOf(".") const ext = base.slice(lastDoutIndex) -console.log(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 052b6dcd1..bfac67a98 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -1,7 +1,7 @@ const minimum = 1; const maximum = 100; -const num = Math.floor(0.999 * (maximum - minimum + 1)) + minimum; +const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // In this exercise, you will need to work out what num represents? // Try breaking down the expression and using documentation to explain what it means diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..510c9b244 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? +// we could add a Single-line comment // .or Multi-line comment /* */ +// Now JavaScript will ignore those lines, and they will not affect our program. \ 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 96fef328b..1ba15d9c2 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -3,3 +3,4 @@ // const age = 33; let age = 33; age = age + 1; + diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index 577112a74..118291092 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,12 +12,21 @@ 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 - // 3 functions in lines 4, 5 and 10 +// 5 functions in 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? +// SyntaxError: missing ) after argument list is Missing comma between arguments // c) Identify all the lines that are variable reassignment statements +// Line 4 :carPrice = Number(carPrice.replaceAll(",", "")); +// Line 5:priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); // d) Identify all the lines that are variable declarations +// Line 1: let carPrice = "10,000"; +// Line 2: let priceAfterOneYear = "8,543"; +// Line 7: const priceDifference = carPrice - priceAfterOneYear; +// Line 8: const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// removes all commas from the string "10,000", making it "10000". and Number(...): converts the resulting string "10000" to a number type 10000. +// so the purpose is to convert a comma-formatted string into a usable numeric value for mathematical operation. diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..5a5ee2aa9 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -11,15 +11,24 @@ 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? +// a) How many variable declarations are there in this program +// 6 variable declared // b) How many function calls are there? +// 1 function in line 10 // c) Using documentation, explain what the expression movieLength % 60 represents +//The remainder (%) operator returns the remainder +// so is divide movieLength by 60 and return the remainder." // 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? +//First, it takes away the extra seconds that don't make up a full minute. +// Then it divides the remaining seconds by 60 to figure out how many complete minutes are in the movie. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// formattedTime // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Yes but not all ,because It handles converting seconds into minutes and hours using integer division and modulus.But with negative integers dose not work correcly +//And does not pad single-digit values with zeroes as wll, It will return something like 2:4:7 instead of 02:04:07, which is not a standard time format. diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..f85b81d0f 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,24 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// Initializes a string that represents an amount in (pence), ending with a `"p"` to signify pence (e.g., `"399p"` = 399 pence). + +//2- const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1); +// this Removes the trailing "p" from the string.so it is substring(0, penceString.length - 1) +// takes all characters except the last. for example For "399p", this returns "399". + +// 3-const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// Pads the number with leading zeros to ensure it is at least 3 digits long. so it helps normalise values like "5" → "005" and "99" → "099". + +// 4 const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); +// Extracts the pounds from the padded string.so it takes all digits except the last two. +//For "399", this gives "3" → meaning 3 pounds. + +//5- const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +// Extracts the last two digits for pence and pads them if necessary. +// substring(length - 2) takes the final 2 characters. +// padEnd(2, "0") ensures the pence string is exactly 2 digits. transfer to two-digit formatting for pence 5 => "05". + +//6- console.log(`£${pounds}.${pence}`); +//this log the final formatted string in pounds and pence using a template literal. +// For "399p", output is: £3.99 From ba1300dc706fa7df48f990df9248c8bfe141774a Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Thu, 18 Sep 2025 16:58:23 +0100 Subject: [PATCH 3/4] refactoring the code by use toStr() for dynamic values instead of % formatting --- Sprint-1/2-mandatory-errors/3.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index 443118aa0..0b65efd8c 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -6,7 +6,8 @@ // Then try updating the expression last4Digits is assigned to, in order to get the correct value // .slice() called on a number, and Numbers don’t have string methods like .slice().it will throw an error // console.log(last4Digits) => TypeError: cardNumber.slice is not a function at Object. -const cardNumber = 4533787178994213; +const cardNumber = 4533787178994213;; // const last4Digits = cardNumber.toString; -const last4Digits = cardNumber % 10000; -console.log(last4Digits) \ No newline at end of file +const numToStr = cardNumber.toString() +const last4Digits = numToStr.slice(-4) +console.log(last4Digits) //===>4213 From c109c5686785e1dd184532588b13f0a1e427dc43 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Thu, 18 Sep 2025 16:59:50 +0100 Subject: [PATCH 4/4] refactoring the code by use toStr() for dynamic values instead of % formatting --- Sprint-1/2-mandatory-errors/3.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index 0b65efd8c..9b9d6036b 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -7,7 +7,6 @@ // .slice() called on a number, and Numbers don’t have string methods like .slice().it will throw an error // console.log(last4Digits) => TypeError: cardNumber.slice is not a function at Object. const cardNumber = 4533787178994213;; -// const last4Digits = cardNumber.toString; const numToStr = cardNumber.toString() const last4Digits = numToStr.slice(-4) -console.log(last4Digits) //===>4213 +// console.log(last4Digits) ===>4213