diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..52083f563 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 = opperator is an assignment opperator that assigns the result of the expression on the right (count+1) to the varibale on the left (count). + so, if count was 0 befor line 3 is excuted it will be 1 after line 3 is excuted. + And if it was 1 it will be 2 after line 3 is excuted. \ 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 47561f617..a36fbfd31 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,7 @@ 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 diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..0f3eb5721 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,8 @@ 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); +console.log(`The dir part of ${filePath} is ${dir}`); +const ext = filePath.slice (lastSlashIndex + 1); +console.log(`The ext part of ${filePath} is ${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..9830066fb 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,5 @@ 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 + +num represents a random integer between the minimum and maximum values (inclusive). \ 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..a78d71701 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 +We don't want the computer to run these 2 lines - how can we solve this problem? + +comment it out using // at the start of each line, or use /* at the start and */ at the end to comment out a block of code. \ 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..c948e6d90 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,10 @@ const age = 33; age = age + 1; +in this code we are trying to reassign the value of age variable by adding 1 to ageTrack. +However, this will result in an error because the age variable is diclared as a constant using the const keyword. +And in javascript, a variable decalred with const cannot be readdigned a new value afteer it has been intialised , + so if we want to reassign the vlaue we have to use let instead. + let age = 33; + age = age +1; + \ 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 e09b89831..ebb2502da 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,6 @@ // 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}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + +Because the const varibale is decalred after it is used in the console={.log} statement and it should be decalred befor to be executed. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..abb1b77c6 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,6 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber .toString().slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +8,9 @@ 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 + +I exepected first that the .slice negative value (-4) will count from the beganning and drob of the first four Numbers. +When I run the code the error was that the cardNumber is not a function, +so I thought that I might need to turn the code into a function, +but when I checked an AI toolbar it showed that .slice is a methode that is used for string and array , + so the card number should turned into a string by add .tostring after the card number and then use the .slice method to get the last 4 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..ac8fa544d 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,4 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + +In javascript variabl names should not start with a Number. \ 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..a97d0732f 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,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 4 function calls in this File, they are in the following lines: +line 1: carPrice.replaceAll(",", "") +line 2: priceAfterOneYear.replaceAll(",", "") +line 3: Number(carPrice.replaceAll(",", "")) +line 4: Number(priceAfterOneYear.replaceAll(",", "")) // 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? // c) Identify all the lines that are variable reassignment statements +there are 4 variables reassignment statements in this file, in the following lines: +line 1 = carPrice = +line 2 = priceAfterOneYear = +line 3 = carPrice = Number(carPrice.replaceAll(",", "")) +line 4 = priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")) + + + // d) Identify all the lines that are variable declarations +there are 2 variable declarations in this file , in : +line 1 : let carPrice = +line 2 : let priceFterOneYear = + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +the expression `Number(carPrice.replaceAll(",", ""))` is doing the following: +1. `carPrice.replaceAll(",", "")` removes all commas from the string value of `carPrice`. +2. `Number(...)` converts the resulting string (which no longer contains commas) into a number. + +This is necessary because the original `carPrice` variable is a string with commas (e.g., "10,000"), and JavaScript cannot perform mathematical operations on strings with commas. + The purpose of this expression is to convert the formatted price string into a numeric value that can be used in calculations. \ 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..2b03d7e20 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,36 @@ 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 6 variable declarations in this programe they are in the following lines: + +const movieLength = 8784; // length of movie in seconds + +const remainingSeconds = movieLength % 60; +const totalMinutes = (movieLength - remainingSeconds) / 60; + +const remainingMinutes = totalMinutes % 60; +const totalHours = (totalMinutes - remainingMinutes) / 60; + +const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; // b) How many function calls are there? +there is 1 function call in this program and it is in the following line: + +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 +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators + +The expression `movieLength % 60` calculates the remainder when `movieLength` is divided by 60. +In this context, it gives the number of seconds that remain after converting the total seconds into minutes and hours. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +The expression assigned to `totalMinutes` calculates the total number of minutes in `movieLength` +by subtracting the remaining seconds and dividing by 60. + // e) What do you think the variable result represents? Can you think of a better name for this variable? +variable result represents the formatted remaining movielength time in hours, minutes, and seconds (HH:MM:SS). // 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 values of movieLength asa long as it is a non- negative integers. diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..7ad1967f6 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,24 @@ console.log(`£${pounds}.${pence}`); // 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); +This line removes the trailing "p" from the pence string, leaving just the numeric value "399". + +3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +This line pads the numeric string with leading zeros to ensure it is at least 3 characters long, resulting in "0399". + +4. const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); +This line extracts the pounds part by taking all characters except the last two (which represent pence), resulting in "03". + +5. const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); +This line extracts the last two characters (representing pence) and ensures they are exactly two digits long by padding with trailing zeros if necessary, resulting in "99". + +6. console.log(`£${pounds}.${pence}`); +This line logs the formatted price in pounds and pence, e.g., £03.99. \ No newline at end of file