diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..0349c90d1 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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 two we see the variable count using the = sign to assign a new value whcih is count previous value + 1 +//the equal sign in line two is assigned operator that stores the new value to the variable diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..dd03693c1 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,7 @@ const 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. -const initials = ``; +const initials = `${firstName[0]}${middleName[0]}${lastName[0]}`; +console.log(initials); // Output CKJ // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..99b0ec639 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,20 @@ 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 = ; +// here we have the variable lastslashIndex already created and identified its position +// stores the position or (index) of "/" in filePath, so this "/" is used as a symbol that storers the number index or a place that seprate dir from base +const dir = filePath.slice(0, lastSlashIndex); +console.log(dir); +// we show our result in the console +const lastDotIndex = filePath.lastIndexOf("."); +// we create a variable and assign its value using the indexOf to be from the dot location +const ext = filePath.slice(lastDotIndex); +// we create a variable where we assign its value using the slice method to be from the dot only -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +// https://www.google.com/search?q=slice+mdn + +console.log(ext); +// we print our result in the console +//slice(startIndex, endIndex) +// means, start here and stop here. we are doing this to seprate the dir from base so we need to tell it where to start and where to end, hence that is why we use slice method, +// my comments needs correcting to look profissional -_- diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..ba0c465c0 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -2,8 +2,20 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; - +console.log(num); // 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 + +//First, const minimum and const maximum are variables +// = is the assignment operator +// what comes after = is what the variable will store +// Math.floor is a method, and here we are telling it that its argument is to use the random method +// and multiply the result before rounding with the maximum and minimum variables we created +// * is a multiplication operator +//To understand what is going on, we need to understand what the floor and random methods do +//Math.floor (rounds down) and then pass it to Math.floor +//We are asking the program to round whatever result we have to the nearest whole number + +//done diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..98ca369bd 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/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 +We don't want the computer to run these 2 lines - how can we solve this problem? +//we use a (//)) to comment it. So the computer doesn't execute the text. diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..140d2fe58 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,9 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; +//a shorthand assignment operator +//yes, += +//an error message shows up: TypeError: Assignment to constant variable. + +//to solve this, we replace const with let to make the variable value changeable, then we run the program diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..08181d5d2 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,8 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? +//The variable was declared after the print function, so the program doesn't know what is cityofBirth. Therefore, it can't perform the print function or execute before declaring the variable + +//to solve this, we reverse the order of both lines so the declaration comes before the print functions -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..914219efd 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,9 +1,13 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); - +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); +//the output is 4213 // 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 +//it won't work because cardNumber is not a string // Then run the code and see what error it gives. +//TypeError: cardNumber.slice is not a function so a string method can't br used with it // Consider: Why does it give this error? Is this what I predicted? If not, what's different? +// The error is caused because card number is a number not a string and toString is a string method so its a different type of data // Then try updating the expression last4Digits is assigned to, in order to get the correct value diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..d0602e7c1 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,11 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const twelveHourClockTime = "8:53pm"; +const twintyFourhourClockTime = "20:53"; + +//we see the error message SyntaxError: Invalid or unexpected token, + +//because variable conventions don't allow numbers to be the start of variable names + +//to solve this issue, we change the numbers to words format + +//done + diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..2652e7ad6 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/3-mandatory-interpret/1-percentage-change.js @@ -1,8 +1,8 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; - +//console.log(typeof carPrice); carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,11 +12,18 @@ 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 FUNCTION calls. Line 4, 5, 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 is SyntaxError: missing ) after argument list +//The error is coming from line 5, column 61. There is nothing seprating the two indexes. A comma can should used to seprate them. // c) Identify all the lines that are variable reassignment statements - +//Two lines 4 and 5 // d) Identify all the lines that are variable declarations - +//four lines 1,2,7,8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +//the expression Number(carPrice.replaceAll(",","")) change the string value to anumber that can be calculated and subtracted +//line 7 needs it so the calculation could work, a string can't be subtractedn from a number so it has to be changed to a number + +//console.log(typeof carPrice); + +//done diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..c52f92706 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -1,5 +1,5 @@ -const movieLength = 8784; // length of movie in seconds - +const movieLength = -1000000; // length of movie in seconds +//not complete const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,20 @@ 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? - +// 6 declarations // b) How many function calls are there? - +// 1 function call in line 10 // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// it represent the number of seconds left over after taking all the complete minutes // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - +//here in the expression we have the movieLength taken from it the number of remainingSeconds, so the operator / 60 represents the conversion of the remaining seconds into minuttes, so we left with complete minutes only // e) What do you think the variable result represents? Can you think of a better name for this variable? - +//the variable result represesnt the length of the film in hours,minutes,and seconds. A better name would be movie time. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//8784 +//if experimented values into the movieLength variable what will happen? Lets start with the following values: +//-10 , 0, 10 , 10000000, -1000000 +//0:0:-10, 0:0:0,0:0:10,2777:46:40, -277:-46:-40 +//ok I notice the result get strange with big negative numbers, so I think the code needs to be modified to restrict using negative values diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..5d58fa9f8 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -2,13 +2,13 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 + penceString.length - 1, ); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 + paddedPenceNumberString.length - 2, ); const pence = paddedPenceNumberString @@ -25,3 +25,37 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +//for this code I struggled with some part so I had to ask copilot to explain some lines of the code to me +//const penceStringWithoutTrailingP = penceString.substring( +// 0, +// penceString.length - 1 +//); +//const penceStringWithoutTrailingP: a string variable is initialised and assigned +// using the = operator to be equal to the value of string penceString when the method substring is used to cut part of the string +//so substring(0, penceString.length - 1);: means use substring method starting from index zero and end before the last character pencestring.length-1 : which is the length of the string +//but how dom we know which index? Its is basically the characters in string where "399p" means we have 4 characters and we want to take 1 +//do 4 - 1 = 3 , so we are removing p character from the string so it becomes 399 +//this line says take the string penceStringWithoutTrailingP and use padStart()string method +// (3, "0"); this means make the string 3 characters at least and if not add o to the beginning +//const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +//this line means take the string paddedPenceNumberString and use the +//substring(0, length - 2) method to remove the last 2 characters , than store +//the value into the pounds variable +//const pounds = paddedPenceNumberString.substring( +// 0, +// paddedPenceNumberString.length - 2 +//); + +//this (paddedPenceNumberString.length - 2) mean find the position of the last 2 characters +//in the string paddedPenceNumberString +//substring()means take the last 2 characters you found from the string +//.padEnd(2, "0"); means if the result is less than two characters , than add zero at +//the end until it reaches two digits +//const pence = paddedPenceNumberString +//.substring(paddedPenceNumberString.length - 2) +// .padEnd(2, "0"); +//this line uses console.log to print the value of ponds and pence +//it uses literal template to find the value of pounds, uses a decimal point , than finds the value of pence +//it also uses a pound sign at the front +// console.log(`£${pounds}.${pence}`);