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 47561f617..231f535b4 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(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..354af57dc 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -14,10 +14,13 @@ 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) // 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..bfac67a98 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) +//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/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 7a43cbea7..1ba15d9c2 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,6 @@ // 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..9b9d6036b 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 numToStr = cardNumber.toString() +const last4Digits = numToStr.slice(-4) +// console.log(last4Digits) ===>4213 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..118291092 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,11 +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 +// 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